Why do I keep getting a null reference? Its not null!
Hello fellow programmers at unity answers, I have been trying to fix this stupid code for like 30 minutes with no results yet.
I have two pieces of UnityScript...... I keep getting a null reference error on line 7 of the first code.....
var script:numberAttempsLogger;
function OnTriggerEnter2D (other : Collider2D) {
if(other.gameObject.tag == "Player"){
script = GetComponent (numberAttempsLogger);
yield WaitForSeconds(1);
script.attempts +=1;
}
}
This code is so simple and idk why it keeps giving me this error
here is the script it is referencing below
var attemptsCounter : GUIText;
var attempts: int = 1;
function Awake() {
DontDestroyOnLoad(Transform.GameObject);
}
function Update() {
attemptsCounter.text = "Attempts : "+ attempts;
}
Am I making a simple syntax mistake?? because im pretty sure i set this up right....if not, can someone please send me the documentation that shows me how to fix it? I have read everything i can find on this
numberAttempsLogger script is attached to the same gameObject as the first script? or is it attached on the player gameObject?
the logger script is attached to an empty gameobject that will be called when the game first starts....so as the player progresses through the levels, the script will still keep count of the amount of deaths in the game
okay, GetComponent will search the components of the current gameObject, so script = GetComponent (numberAttempsLogger); will try and check for the script on the current gameObject the first script is attached to. $$anonymous$$aybe you want to change this line to be script = emptyObj.GetComponent (numberAttempsLogger); where emptyObj is the empty gameobject reference your script is attached to. Hope I am not misunderstanding your setup.