Answered Opening Doors with script

alppi00a

Member
Joined
Mar 18, 2020
Messages
11
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

  • DoorOpen.txt
    3 KB · Views: 22
  • PlayerRay.txt
    943 bytes · Views: 20
CodeTags.gif
 
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:
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.
 
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.
 
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 :)
 
Back
Top Bottom