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 :slight_smile:

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;
			case (2):
				Number.renderer.material = Num2;
				TurnLightOnOff = true;
			case (3):
				Number.renderer.material = Num1;
				TurnLightOnOff = true;
			case (4):
				Number.renderer.material = GO;
				BckLight.renderer.material = GreenLight;
				GameObject.Find("GameController").GetComponent(RaceSequencer).StartTimerEnd = true;
	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);

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.