- Home /
Door script problem
When the door is not visible in the visibility zone, it is necessary to move the cursor away from the object and there is an opening
Script door
using UnityEngine; using System.Collections;
public class DoorScript : MonoBehaviour {
//GENERAL SETTINGS
[Header("General Settings")]
[Tooltip("How close the player has to be in order to be able to open/close the door.")]
public float Reach = 4.0F;
[HideInInspector] public bool InReach;
//UI SETTINGS
[Header("UI Settings")]
[Tooltip("The image or text that will be shown whenever the player is in reach of the door.")]
public GameObject TextPrefab; //A text element to display when the player is in reach of the door.
[HideInInspector] public GameObject TextPrefabInstance; //A copy of the text prefab to prevent data corruption.
[HideInInspector] public bool TextActive;
public AudioSource openDoor;
public AudioSource closeDoor;
public bool open;
public bool close;
public bool inTrigger;
void Start ()
{
}
void Update()
{
//Set origin of ray to 'center of screen' and direction of ray to 'cameraview'.
Ray ray = Camera.main.ViewportPointToRay(new Vector3 (0.5F, 0.5F, 0F));
RaycastHit hit; //Variable reading information about the collider hit.
//Cast ray from center of the screen towards where the player is looking.
if (Physics.Raycast(ray, out hit, Reach))
{
if (hit.collider.tag == "Door")
{
InReach = true;
//Display the UI element when the player is in reach of the door.
if(TextActive == false && TextPrefab != null)
{
TextPrefabInstance = Instantiate(TextPrefab);
TextActive = true;
TextPrefabInstance.transform.SetParent(transform,true); //Make the player the parent object of the text element.
}
//Give the object that was hit the name 'Door'.
GameObject Door = hit.transform.gameObject;
//Get access to the 'Door' script attached to the object that was hit.
Door dooropening = Door.GetComponent<Door>();
{
//Open/close the door by running the 'Open' function found in the 'Door' script.
if (dooropening.Running == true) StartCoroutine (hit.collider.GetComponent<Door>().Open());
}
}
else
{
InReach = false;
//Destroy the UI element when Player is no longer in reach of the door.
if(TextActive == true)
{
DestroyImmediate(TextPrefabInstance);
TextActive = false;
}
}
}
else
{
InReach = false;
//Destroy the UI element when Player is no longer in reach of the door.
if(TextActive == true)
{
DestroyImmediate(TextPrefabInstance);
TextActive = false;
}
}
if (inTrigger)
{
if (close)
{
{
if (Input.GetKeyDown(KeyCode.E))
{
open = true;
close = false;
openDoor.Play();
}
}
}
else
if (open)
{
if (Input.GetKeyDown(KeyCode.E))
{
close = true;
open = false;
closeDoor.Play();
}
}
}
if (open)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, -90.0f, 0.0f), Time.deltaTime * 200);
}
if (close)
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, 0.0f, 0.0f), Time.deltaTime * 200);
}
}
}
Help me decide
Error here (NullReferenceException: Object reference not set to an instance of an object) {
if (dooropening.Running == true) StartCoroutine (hit.collider.GetComponent<Door>().Open());
}
Answer by merkaba48 · Jul 04, 2017 at 07:12 PM
I don't understand your problem as you've described it, but fix that nullref exception first in case that's the cause of your issue.
As you've already confirmed there is a hit, for some reason it's probably not finding the Door component. Is the Door script actually on the gameobject with the collider, or is it a child, or parent? If a parent, then use hit.collider.transform.parent.GetComponent<Door>().Open()
, or if it's a child, use hit.collider.GetComponentInChildren<Door>().Open()
.
Alternatively, it's possible you accidentally have a collider in the scene with the 'Door' tag but no Door component.