-1
\$\begingroup\$

I have 2 objects in scene. When pillar is bellow target, pillar object needs to animate. I get this error: "NullReferenceException: Object reference not set to an instance of an object".

var pillar : GameObject;
var target : GameObject;

public var triggered : boolean = false;

function OnTriggerEnter() { triggered= true; }
function OnTriggerExit()  { triggered= false;}

function Update(){
    if (triggered && Input.GetKeyDown(KeyCode.JoystickButton1)){
        if(target.transform.position.y > pillar.transform.position.y){
            pillar = GameObject.Find("Pillars");
            pillar.animation.Play();
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Learn to use the debugger. Also reading the error message helps. It tells you exactly what's wrong and contains a line number to tell you exactly where it's going wrong. In this case it'll tell you you're trying to access a member variable of a null object. Just a quick look at your code makes it clear that you're trying to use pillar.transform.position.y before telling the program what pillar is. Programs execute top to bottom. \$\endgroup\$ – MichaelHouse Aug 6 '14 at 18:06
1
\$\begingroup\$

In your second if you access the pillar and you set it to an object.

When you try to access the pillar-Object, it never has been set and as such is initialized with null. This is your error.

//first if
{
    tempPillar = GameObject.FindGameobjectWithTag ("Pillars");
    //second if with tempPillar!
    {
        pillar =tempPillar; 
        //play Animation
    }
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.