-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\$

closed as off-topic by Anko, jhocking, bummzack, Seth Battin, congusbongus Aug 12 '14 at 8:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Anko, jhocking, bummzack, Seth Battin, congusbongus
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\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.