- Home /
Null Reference - Help
Hi there, I am getting a weird "null reference object reference not set an instance of an object" error and am unable to stop the timer on collision. This is the code with the error and it's code is linked to another.
function Start () {
}
function Update () {
}
function OnCollisionEnter(o:Collision){
if(o.gameObject.name=="LOL"){
var charScript:MovewithRaycast=GameObject.Find("CharContainer").GetComponent("MovewithRaycast") as MovewithRaycast;
if(charScript.isShowingMission2()){
var t:Timer;
t=GameObject.Find("Timer1").GetComponent("Timer1") as Timer;
var timeTaken:int=t.stopTimer();
Debug.Log("time taken was "+timeTaken);
}
}
}
The error seems to be coming from the "var timeTaken:int=t.stopTimer()" bit. and Here is the code it's linked up to it
private var doTimer:boolean;
private var startTime:float;
private var elapsedTime:float;
function Start () {
doTimer=false;
}
function Update () {
if(doTimer){
elapsedTime=Time.time-startTime;
GetComponent(GUIText).text = (elapsedTime).ToString("f2");
}
}
function startTimer(){
startTime=Time.time;
doTimer=true;
}
function stopTimer(){
doTimer=false;
return parseInt(elapsedTime);
}
any help is greatly appreciated.
First, use $$anonymous$$athf.FloorToInt(elapsedTime)
ins$$anonymous$$d of parseInt(elapsedTime)
. Check the elapsedTime value and if the Update()
of your second script is called, I suggest you to call Update()
at stopTimer()
start.
It doesn't work.. It still gives the null.. and I already have a replica of the same script working on another object, with $$anonymous$$or changes to not confuse unity.. Why is it working?.. This is the coding for the working one..
#pragma strict
function Start () {
}
function Update () {
}
function OnCollisionEnter(o:Collision){
if(o.gameObject.name=="EndBox"){
var charScript:$$anonymous$$ovewithRaycast=GameObject.Find("CharContainer").GetComponent("$$anonymous$$ovewithRaycast") as $$anonymous$$ovewithRaycast;
if(charScript.isShowing$$anonymous$$ission()){
var t:Timer;
t=GameObject.Find("Timer").GetComponent("Timer") as Timer;
var timeTaken:int=t.stopTimer();
Debug.Log("time taken was "+timeTaken);
}
}
}
and the script linked to it
private var doTimer:boolean;
private var startTime:float;
private var elapsedTime:float;
function Start () {
doTimer=false;
}
function Update () {
if(doTimer){
elapsedTime=Time.time-startTime;
GetComponent(GUIText).text = (elapsedTime).ToString("f2");
}
}
function startTimer(){
startTime=Time.time;
doTimer=true;
}
function stopTimer(){
doTimer=false;
return parseInt(elapsedTime);
}
Insert a debug state between line 16 and 17 to check if 't' is null. If it is, check to make absolutely you have a game object named 'Timer1' there is a component named 'Timer1' attached to that game object. Note the string must match exactly. If you find both are correct, then do a search (type in the search field at the top of the Hierarchy) for 'Timer1' to make sure you don't have two game objects named 'Timer1'...one with a 'Timer1' component, and one without.
There aren't any other elements named Timer 1 except for the GUIText that has the script Timer1.js on it.. I have really been bashing my head with this problem for hours now..I wonder why the replica code of it works flawlessly though?
Note in the preceding comment you called it 'Timer 1' with a space. But in the code above code you called it 'Timer1' without a space. I don't know if this is just a mistake in the comment or if this is the problem
Your answer
