Question Opening Doors with script

alppi00a

Member
Joined
Mar 18, 2020
Messages
9
Programming Experience
Beginner
Can anyone tell me why i have NullReferenceException in this code.
(If you have any hints how to make doors work better in unity i'm open to ideas.)
Thank you.
 

Attachments

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,141
Location
UK
Programming Experience
10+
No. I am not downloading that just to read it.

Post your code on the forums in code tags, like everyone else. Then we will check it.
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,141
Location
UK
Programming Experience
10+
CodeTags.gif
 

alppi00a

Member
Joined
Mar 18, 2020
Messages
9
Programming Experience
Beginner
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

    public class DoorOpen : MonoBehaviour
    {
        public PlayerRay playerray;
        public Vector3 AfterAngles;
        public Animator animator;
        public Vector3 CurrentAngles;
        public GameObject Door;
        public float x;
        public float y;
        public float z;
        public float x1;
        public float y1;
        public float z1;
        public bool DoorIsOpen = false;
        public bool DoorIsClosed = true;
        public bool PlayerIsNear = false;
        public Text InfoText;
        public GameObject TextParent;
        // Start is called before the first frame update
        public void Start()
        {
            TextParent.SetActive(false);
            Door = this.gameObject;
            animator = GetComponent<Animator>();
        }

        // Update is called once per frame
        public void Update()
        {
            if (Input.GetKeyDown(KeyCode.E) && DoorIsOpen == false && PlayerIsNear == true)
            {
                animator.SetTrigger("OpenDoor");
                x = CurrentAngles.x;
                y = CurrentAngles.y;
                z = CurrentAngles.z;
                Door.GetComponent<Transform>().eulerAngles = CurrentAngles;
                Door.GetComponent<Transform>().eulerAngles.Set(x, y -= 90, z);
                Door.GetComponent<Transform>().eulerAngles = AfterAngles;
                x1 = AfterAngles.x;
                y1 = AfterAngles.y;
                z1 = AfterAngles.z;
            }
            if (Input.GetKeyDown(KeyCode.E) && DoorIsOpen == true && PlayerIsNear == true)
            {
                animator.SetTrigger("CloseDoor");
                x = CurrentAngles.x;
                y = CurrentAngles.y;
                z = CurrentAngles.z;
                Door.GetComponent<Transform>().eulerAngles = CurrentAngles;
                Door.GetComponent<Transform>().eulerAngles.Set(x, y += 90, z);
                Door.GetComponent<Transform>().eulerAngles = AfterAngles;
                x1 = AfterAngles.x;
                y1 = AfterAngles.y;
                z1 = AfterAngles.z;
            }
            if (playerray.IsOpen == true)
            {
                //Debug.Log("we hit" + hit.collider.name + " " + hit.point);
                rayopen();
            }
            else
            {
                rayclose();
            }
        }
        public void open()
        {
            DoorIsOpen = true;
            DoorIsClosed = false;
        }
        public void close()
        {
            DoorIsOpen = false;
            DoorIsClosed = true;
        }
        public void rayopen()
        {
            TextParent.SetActive(true);
            PlayerIsNear = true;
            Debug.Log("welcome");
            InfoText.text = ("press E");
        }
        public void rayclose()
        {
            PlayerIsNear = false;

            TextParent.SetActive(false);
        }
    }
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class PlayerRay : MonoBehaviour
    {
        public bool IsOpen;
        public GameObject DoorParent;
        public DoorOpen dooropen;
        public LayerMask door;
        private Camera cam;
        // Start is called before the first frame update
        private void Start()

        {
            dooropen = DoorParent.GetComponentInChildren<DoorOpen>();
            cam = Camera.main;
        }
        // Update is called once per frame
        private void Update()
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 5, door))
            {
                Debug.Log("we hit" + hit.collider.name + " " + hit.point);
                //dooropen.rayopen();
                IsOpen = true;
            }
            else
            {
                //dooropen.rayclose();

                IsOpen = false;
            }
        }
    }
 
Last edited by a moderator:

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,141
Location
UK
Programming Experience
10+
You're welcome. Now this is where you tell us where the error is, and what you are expecting to happen; that is obviously not happening. Please be clear and descriptive. Dumping a wad of code and expecting us to zip through it is likely not going to happen. So please specifically narrow down the problem to a line number and explain your problem, and we will try provide you with a solution.
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,141
Location
UK
Programming Experience
10+
Is the problem on line 23 on the second block of code?
RaycastHit hit; ? Or somewhere else ???
 

alppi00a

Member
Joined
Mar 18, 2020
Messages
9
Programming Experience
Beginner
I get NullReferenceException in DoorOpen script in line 61 when i try to refer to other scripts boolean IsOpen.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
Considering that nothing sets playerray to a valid value within that class why would you expect it to not be null?

So you need to add code before line 61 which will check all the player rays within the world is nearest and then use that instance to initialize the variable.
 

alppi00a

Member
Joined
Mar 18, 2020
Messages
9
Programming Experience
Beginner
Considering that nothing sets playerray to a valid value within that class why would you expect it to not be null?

So you need to add code before line 61 which will check all the player rays within the world is nearest and then use that instance to initialize the variable.
Thanks i try that :)
 
Top Bottom