Question by
TheZestySquid · Sep 14, 2016 at 09:14 AM ·
nullreferenceexceptionreferencenull
I don't understand NullReferenceException: Object reference not set to an instance of an object
So I'm trying to make a switch to open a door in my fps and I'm getting a null reference when calling on another script "PlayerCasting" Here's my first script the one giving off the error:
import UnityEngine.UI;
var TextDisplay : GameObject;
var TheDistance : float = PlayerCasting.DistanceFromTarget;
function Update () {
TheDistance = PlayerCasting.DistanceFromTarget;
}
function OnMouseOver () {
if (TheDistance <= 2) {
TextDisplay.GetComponent.<Text>().text = "Press Button";
}
}
function OnMouseExit () {
TextDisplay.GetComponent.<Text>().text = "";
}
The error is occurring at js.4 or the line that starts with var TheDistance
In the line, I am calling on TheDistance which is in the other script "PlayerCasting".
Here is the other script that I'm trying to reference:
static var DistaceFromTarget : float;
var ToTarget : float;
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) {
ToTarget = hit.distance;
DistanceFromTarget = ToTarget;
}
}
What did it do wrong? Any help is appreciated!
Thanks!
-Brendan
Comment
Is the typo "DistaceFromTarget" only in your question or is it in your script as well?