Question by
shadobox255 · Sep 05, 2016 at 11:18 PM ·
error message
NullReferenceException: Object reference not set to an instance of an object
I keep getting an error stating, NullReferenceException: Object reference not set to an instance of an object opendoor.Update () (at Assets/opendoor.cs:26). I'm using this for my door to open and close it. Can someone tell me what's wrong with this?
using UnityEngine;
using System.Collections;
public class opendoor : MonoBehaviour {
public float doorOpenAngle = 90.0f;
public float doorCloseAngle = 0.0f;
public float doorAnimSpeed = 2.0f;
private Quaternion doorOpen = Quaternion.identity;
private Quaternion doorClose = Quaternion.identity;
private Transform playerTrans = null;
public bool doorStatus = false; //false is close, true is open
private bool doorGo = false; //for Coroutine, when start only one
void Start() {
doorStatus = false; //door is open, maybe change
//Initialization your quaternions
doorOpen = Quaternion.Euler (0, doorOpenAngle, 0);
doorClose = Quaternion.Euler (0, doorCloseAngle, 0);
//Find only one time your player and get him reference
playerTrans = GameObject.Find ("Player").transform;
}
void Update() {
//If press F key on keyboard
if (Input.GetKeyDown(KeyCode.F) && !doorGo) {
//Calculate distance between player and door
if (Vector3.Distance(playerTrans.position, this.transform.position) < 5f) {
if (doorStatus) { //close door
StartCoroutine(this.moveDoor(doorClose));
} else { //open door
StartCoroutine(this.moveDoor(doorOpen));
}
}
}
}
public IEnumerator moveDoor (Quaternion dest)
{
doorGo = true;
//Check if close/open, if angle less 4 degree, or use another value more 0
while (Quaternion.Angle (transform.localRotation, dest) > 4.0f) {
transform.localRotation = Quaternion.Slerp (transform.localRotation, dest, Time.deltaTime * doorAnimSpeed);
//UPDATE 1: add yield
yield return null;
}
//Change door status
doorStatus = !doorStatus;
doorGo = false;
//UPDATE 1: add yield
yield return null;
}
}
Comment
What is the exact line that the console shows to you?, because, maybe you are not searching using the right method, you can checking it at opening your code file and find that specific line.
Answer by hexagonius · Sep 06, 2016 at 03:30 PM
Check this out:
https://docs.unity3d.com/Manual/NullReferenceException.html
Your answer
Follow this Question
Related Questions
UCE0001: ';' expected. Insert a semicolon at the end. 1 Answer
0xc000007b error on 64 bit machine? 1 Answer
Position is infinite 0 Answers