- Home /
NullReferenceException in follow function
@HideInInspector var flw : Vector3 = Vector3.zero;
var leader : GameObject;
var enemy : GameObject;
function Start ()
{
leader = GameObject.Find("Leader"+enemy.tag);
}
function Update () {
if (enemy.name!=leader.name)
{
enemy.transform.position = follow(leader);
}
}
function follow(lead :GameObject)
{
enemy=this.gameObject;
enemy.transform.LookAt(lead.transform.position);
var delta = lead.transform.position - enemy.transform.position;
delta.Normalize();
flw= enemy.transform.position + (delta * Enemymove.moveSpeed);
return flw;
}
here's my script Every time the leader game object (and consequently the lead game object in the follow function) is called in game, the game crashes and I get a NullReferenceException at all the lines which use the object. Does anybody have any idea what I'm doing wrong?
Is leader equals null after leader = GameObject.Find("$$anonymous$$er"+enemy.tag);
?
What is the value of enemy.tag ?
Did you check that you have a game object with the name "$$anonymous$$er"+enemy.tag in the current scene ?
If the game object "leader" already exists when this script starts, I advise you to do the same that you did with enemy, which means you drag and drop the game object into the script variable in the inspector.
looks like you're tryin to access enemy before you set what it is, also if the enemy has the script on it i wouldn't set it to a variable because you don't need to
Your answer
Follow this Question
Related Questions
NullReferenceException help 1 Answer
Move instantiated object towards player's posistion 3 Answers
Get Vector3 position of player? 0 Answers
Trying to make a simple inventory: 0 Answers
Using Scripts in AssetBundles 0 Answers