NullReferenceException: Object reference not set to an instance of an object

Hey Guys, I get this error when I pull my scripts in and after hours of trawling the forums I can’t find the solution. Hopefully you can help.

The errors occurs on 1 and 2.

private var Target1 : Transform;
private var Target1Script : BattleIvysaur = Target1.GetComponent(BattleIvysaur);

Full Script is Below, sorry it’s long but I didn’t know how much info you’d need

private var Target1 : Transform;
private var Target1Script : BattleIvysaur = Target1.GetComponent(BattleIvysaur);
private var Target2 : Transform;
private var Target2Script : Battle = Target2.GetComponent(Battle);
private var Target3 : Transform;
private var Target3Script : Battle = Target3.GetComponent(Battle);
private var Target4 : Transform;
private var Target4Script : Battle = Target4.GetComponent(Battle);
private var Target5 : Transform;
private var Target5Script : Battle = Target5.GetComponent(Battle);
private var Target6 : Transform;
private var Target6Script : Battle = Target6.GetComponent(Battle);
private var Target7 : Transform;
private var Target7Script : Battle = Target7.GetComponent(Battle);
private var Health = 100;
static var Damage = 10;
static var MyName = "Wartortle";
private var attackRepeatTime = 1;
static var Pokelement = "WATER";
private var AttackRange = 300;
private var attackTime = 1;

function UPDATE()
{
	var Distance = 0;
	Distance = GetLow();
	if(Distance < AttackRange)
	{
		if(Time.time > attackTime)
		{
			var DamageMultiplyer = 1;
			var OpPokElement = "";
			switch (Distance)
			{
			 	case Vector3.Distance(Target1.position, transform.position):
			 		OpPokElement = Target1Script.Pokelement;
					if(OpPokElement == "GRASS")
					{
						DamageMultiplyer = 0.5;
					}
					else if(OpPokElement == "WATER")
					{
						DamageMultiplyer = 0.5;
					}
					else if (OpPokElement == "FIRE")
					{
						DamageMultiplyer = 2;
					}
					else 
					{
						DamageMultiplyer = 1;
					}
					Target1.SendMessage("TakeDamage", Damage*DamageMultiplyer);
					attackTime = Time.time + attackRepeatTime;
					Debug.Log(MyName+" hit "+Target1Script.MyName+" for "+(Damage*DamageMultiplyer)+" Damage!!");
			        break;
			    case Vector3.Distance(Target2.position, transform.position):
			    	OpPokElement = Target2Script.Pokelement;
					if(OpPokElement == "GRASS")
					{
						DamageMultiplyer = 0.5;
					}
					else if(OpPokElement == "WATER")
					{
						DamageMultiplyer = 0.5;
					}
					else if (OpPokElement == "FIRE")
					{
						DamageMultiplyer = 2;
					}
					else 
					{
						DamageMultiplyer = 1;
					}
					Target2.SendMessage("TakeDamage", Damage*DamageMultiplyer);
					attackTime = Time.time + attackRepeatTime;
					Debug.Log(MyName+" hit "+Target2Script.MyName+" for "+(Damage*DamageMultiplyer)+" Damage!!");
			        break;
			    case Vector3.Distance(Target3.position, transform.position):
			    	OpPokElement = Target1Script.Pokelement;
					if(OpPokElement == "GRASS")
					{
						DamageMultiplyer = 0.5;
					}
					else if(OpPokElement == "WATER")
					{
						DamageMultiplyer = 0.5;
					}
					else if (OpPokElement == "FIRE")
					{
						DamageMultiplyer = 2;
					}
					else 
					{
						DamageMultiplyer = 1;
					}
					Target3.SendMessage("TakeDamage", Damage*DamageMultiplyer);
					attackTime = Time.time + attackRepeatTime;
					Debug.Log(MyName+" hit "+Target3Script.MyName+" for "+(Damage*DamageMultiplyer)+" Damage!!");
			        break;
			    case Vector3.Distance(Target4.position, transform.position):
			    	OpPokElement = Target1Script.Pokelement;
					if(OpPokElement == "GRASS")
					{
						DamageMultiplyer = 0.5;
					}
					else if(OpPokElement == "WATER")
					{
						DamageMultiplyer = 0.5;
					}
					else if (OpPokElement == "FIRE")
					{
						DamageMultiplyer = 2;
					}
					else 
					{
						DamageMultiplyer = 1;
					}
					Target4.SendMessage("TakeDamage", Damage*DamageMultiplyer);
					attackTime = Time.time + attackRepeatTime;
					Debug.Log(MyName+" hit "+Target4Script.MyName+" for "+(Damage*DamageMultiplyer)+" Damage!!");
			        break;
			    case Vector3.Distance(Target5.position, transform.position):
			    	OpPokElement = Target5Script.Pokelement;
					if(OpPokElement == "GRASS")
					{
						DamageMultiplyer = 0.5;
					}
					else if(OpPokElement == "WATER")
					{
						DamageMultiplyer = 0.5;
					}
					else if (OpPokElement == "FIRE")
					{
						DamageMultiplyer = 2;
					}
					else 
					{
						DamageMultiplyer = 1;
					}
					Target5.SendMessage("TakeDamage", Damage*DamageMultiplyer);
					attackTime = Time.time + attackRepeatTime;
					Debug.Log(MyName+" hit "+Target5Script.MyName+" for "+(Damage*DamageMultiplyer)+" Damage!!");
			        break;
			    case Vector3.Distance(Target6.position, transform.position):
			    	OpPokElement = Target6Script.Pokelement;
					if(OpPokElement == "GRASS")
					{
						DamageMultiplyer = 0.5;
					}
					else if(OpPokElement == "WATER")
					{
						DamageMultiplyer = 0.5;
					}
					else if (OpPokElement == "FIRE")
					{
						DamageMultiplyer = 2;
					}
					else 
					{
						DamageMultiplyer = 1;
					}
					Target6.SendMessage("TakeDamage", Damage*DamageMultiplyer);
					attackTime = Time.time + attackRepeatTime;
					Debug.Log(MyName+" hit "+Target6Script.MyName+" for "+(Damage*DamageMultiplyer)+" Damage!!");
			        break;
			    case Vector3.Distance(Target7.position, transform.position):
			    	OpPokElement = Target7Script.Pokelement;
					if(OpPokElement == "GRASS")
					{
						DamageMultiplyer = 0.5;
					}
					else if(OpPokElement == "WATER")
					{
						DamageMultiplyer = 0.5;
					}
					else if (OpPokElement == "FIRE")
					{
						DamageMultiplyer = 2;
					}
					else 
					{
						DamageMultiplyer = 1;
					}
					Target7.SendMessage("TakeDamage", Damage*DamageMultiplyer);
					attackTime = Time.time + attackRepeatTime;
					Debug.Log(MyName+" hit "+Target7Script.MyName+" for "+(Damage*DamageMultiplyer)+" Damage!!");
			        break;
			    default:
			        break;
			}
		}
	}
}

function TakeDamage(deeps : int)
{
	Health = Health-deeps;
}

function GetLow()
{
	var Distance = Vector3.Distance(Target1.position, transform.position);
	var ThierName = "";
	if (Vector3.Distance(Target2.position, transform.position)>Distance) 
	{
		Distance = Vector3.Distance(Target2.position, transform.position);
	}
	if (Vector3.Distance(Target3.position, transform.position)>Distance) 
	{
		Distance = Vector3.Distance(Target3.position, transform.position);
	}
	if (Vector3.Distance(Target4.position, transform.position)>Distance) 
	{
		Distance = Vector3.Distance(Target4.position, transform.position);
	}
	if (Vector3.Distance(Target5.position, transform.position)>Distance) 
	{
		Distance = Vector3.Distance(Target5.position, transform.position);
	}
	if (Vector3.Distance(Target6.position, transform.position)>Distance) 
	{
		Distance = Vector3.Distance(Target6.position, transform.position);
	}
	if (Vector3.Distance(Target7.position, transform.position)>Distance) 
	{
		Distance = Vector3.Distance(Target7.position, transform.position);
	}
	return Distance;
}

According to the first two lines you showed us, you declare Target1 as a variable of type Transform but never set it to anything.

Then you try to reference it in the next line.

Thats your problem right there. I would guess you were expecting to set it in the editor, but thats too late.
It needs a value as soon as the second variable is declared.

The simplest solution is not to use an initializer with the second variable. Instead, set it in your Start routine so it doesn’t try to do it until you run the scene.

Target1 needs to be set before you can call Target1.GetComponent(...).

You’ve declared a variable, but you haven’t yet given it a value.

Notice the subtle distinction between the following:

  • Robot Assistant, open the mail in my personal box.
  • Robot Assistant, check to see if there’s mail in my personal box. If you find any, open it.

To a person, the second instructions might sound equivalent and pedantic. To a computer, the second instructions are profoundly different and much more thorough.

Computers are incredibly specific; most programs will not check if an operation is safe unless you tell them to!

So! You have two options:

  • Always make sure that Target1 has a value. You could make it public, then set it in the inspector.
  • Always check that Target1 is not null before accessing it.

In your specific case, the first option probably involves less extra code. In the long run, the second option is usually good practice.

After that, I would recommend moving those GetComponent calls into an Awake or Start function that will be called once the game starts; it’s much easier to control the order of operations and avoid unusual errors, that way.

Finally, you might check into tutorials for arrays and loops. They’re a bit intimidating at first, but they make code like this a lot easier to manage.

Thank you both, got it sorted now, can’t believe I missed that XD

Really appreciate the help,
Ross