I am making a Game in unity for an University assignment, and so far I have been doing good enough. I have made quite a lot of research every time i have come against an obstacle, and thus far I have solved all of my previous issues. However, this time, I’m having an issue with something I have already found the answer for, and I can’t seem to find the mistake anywhere, no matter how hard I try to do so.
I am trying to access a variable from a different script, a Boolean to be specific, and while I have read how to do this, I can’t seem to make it work.
The two scripts (not the final code, just what I was using to test if I could use the variable or not) are as follow:
#pragma strict
var crosshair : Texture2D;
var welcomeMessage : WelcomeMessage;
function Awake(){
welcomeMessage = GetComponent(WelcomeMessage);
}
function Start () {
}
function Update () {
welcomeMessage.messageDone = true;
}
function OnGUI(){
GUI.Label (Rect (Screen.width/2-250, Screen.height/2-150, 512, 512), crosshair);
}
#pragma strict
var firstMessage : Texture2D;
var secondMessage : Texture2D;
var thirdMessage : Texture2D;
var fourthMessage : Texture2D;
var fifthMessage : Texture2D;
var sixthMessage : Texture2D;
var seventhMessage : Texture2D;
var messageLifeSpan : float = 1.0f;
var messageTimer : float = 0.0f;
public var messageDone : boolean = false;
function Start () {
}
function Update () {
//SetUp Timer to keep track of time.
messageTimer += Time.deltaTime;
}
function OnGUI (){
//wait 1 second, then show Message No1 for 8 seconds
if (messageTimer > 1 && messageTimer < 9)
GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), firstMessage);
//wait another 2 seconds, then show message No2 for 12 seconds.
else if (messageTimer > 11 && messageTimer < 27)
GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), secondMessage);
//wait another second, then show message No3 for 12 seconds.
if (messageTimer > 29 && messageTimer < 45)
GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), thirdMessage);
//wait another second, then show message No4 for 12 seconds.
else if (messageTimer > 47 && messageTimer < 63)
GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), fourthMessage);
//wait another second, then show message No5 for 8 seconds.
else if (messageTimer > 65 && messageTimer < 73)
GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), fifthMessage);
//wait another second, then show message No6 for 12 seconds.
if (messageTimer > 75 && messageTimer < 91)
GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), sixthMessage);
//wait another second, then show message No7 for 12 seconds.
else if (messageTimer > 93 && messageTimer < 109)
GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), seventhMessage);
}
The error I am getting is:
NullReferenceException: Object reference not set to an instance of an object
Crosshair.Update () (at Assets/Scripts/Game/All Difficulties/All Levels/Crosshair.js:17)
And I cant understand why, I am using the format the Script Reference uses. Can anyone help me please?, I really need to be able to use variable from different scripts with ease.