- Home /
Errors while assigning material to other object's by scripting.
I wanted to make a Start Race Counter thing in Unity, it was supposed to be two rotating rings with changing light inside of them and counter which shows "3,2,1,GO" and from my view it should work as it is now... but it does not and I have no idea why becouse it seem to be written good... I get this error although everything is assigned in Inspector as it should be:
NullReferenceException: Object reference not set to an instance of an object "line ~40"
Big thanks in advance :)
var start : boolean = false;
private var TurnLightOnOff : boolean = false;
private var Counter : float = 0;
private var Timer : float = 0;
private var TimerFull : float = 0;
var GO : Material;
var Num1 : Material;
var Num2 : Material;
var Num3 : Material;
var GreenLight : Material;
var PurpleLight : Material;
var OuterRing : GameObject;
var InsideRing : GameObject;
var Number : GameObject;
var BckLight : GameObject;
function Update () {
OuterRing.transform.Rotate (Vector3(-2,0,0));
InsideRing.transform.Rotate (Vector3(2,0,0));
if (start == true){
Timer += Time.deltaTime;
TimerFull += Time.deltaTime;
if (TimerFull >= 2){
if (Timer >= 1){
Counter ++;
Timer = 0;
}
}
}
switch (Counter){
case (1):
Number.renderer.material = Num3;
TurnLightOnOff = true;
return;
case (2):
Number.renderer.material = Num2;
TurnLightOnOff = true;
return;
case (3):
Number.renderer.material = Num1;
TurnLightOnOff = true;
return;
case (4):
Number.renderer.material = GO;
BckLight.renderer.material = GreenLight;
GameObject.Find("GameController").GetComponent(RaceSequencer).StartTimerEnd = true;
return;
}
if (TurnLightOnOff && TimerFull < 5){
BckLight.renderer.material = GreenLight;
if (Timer >= 0.5){
BckLight.renderer.material = PurpleLight;
TurnLightOnOff = false;
}
}
if (TimerFull >= 8){
transform.Translate (Vector3(0,0,-1));
Destroy (gameObject, 2);
}
}
To which line in the code you posted does the error take you too?
Answer by rutter · Apr 07, 2012 at 08:31 PM
A null reference exception usually means you're trying to access a reference which doesn't point to anything.
Take this line as an example:
Number.renderer.material = Num2;
A null reference exception here could mean that Number
is null, that its `renderer` property returned null, or that the renderer's `material` property returned null. You should check all three possibilities, in roughly that order.
Number
seems to be a variable you're setting in the inspector. Make sure that you've done that.
Number.renderer
will return null if Number
doesn't have a renderer directly attached. Check that. Remember that you can also use `GetComponentInChildren()` if the renderer is not directly attached.
Number.renderer.material
will return null if the renderer does not have a material set (usually in the inspector). Make sure that you've done that.
You should repeat these checks for every line that's erroring out as you've described.
But if I say that: Renderer is directly attached to object and material is assigned where it should be and number is assigned too? Is there any other possibility of what can be wrong?
Some reference has to be null at the particular moment you access it, or you wouldn't be getting that exception. We know empirically that something is wrong, and that it involves a null reference.
Only question left is: which reference, and why? Not necessarily an easy question, but it's good to know where we're at.
Open the editor's console window.
Run your game, reproduce the null reference exception.
Use the log output to identify the line of code that errors out.
Go to that line, identify all references on that line.
Immediately before that line, add some
Debug.Log()
statements to print out those refs.Run your game, reproduce the null reference exception.
Check the log output again. See which variable was null, or which log statement errored out.
Using that extra knowledge, troubleshoot.
Assu$$anonymous$$g the problem line is the one I quoted above, your debug calls could look something like this:
Debug.Log(Number);
Debug.Log(Number.renderer);
Debug.Log(Number.renderer.material);
Debug.Log(Num2);
Number.renderer.material = Num2;
If you're comfortable with it, it might be much faster to just use $$anonymous$$onoDevelop's built-in debugger, add a breakpoint at the problem line, and use that to check out the program's state just before the exception happens... that's more powerful, but also a steeper learning curve.
Hang on a $$anonymous$$ute ... Number is a class, so na$$anonymous$$g it as a variable should cause a NullReferenceException ...
I'm not even sure what it's a class for though ... I've never seen it used before ...
EDIT
Aha! It's for doubles! I've really wanted to know what doubles were for a while though ...
There's your problem! Use something other than Number as your variable name.
Your answer
Follow this Question
Related Questions
Changing GameObject texture? 4 Answers
just a simple question.. 1 Answer
Vertex Fragment Shaders are BROKEN? 2 Answers
Fix this script? 0 Answers
Expand script to stop camera from following player 2 Answers