Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The following javascript is attached to a gameObject.

var instance : GameObject;

class eg_class   {   
    function eg_func(){    
       var thePrefab : GameObject;
        instance = Instantiate(thePrefab);
    }
}

Error,

Unknown identifier: 'instance'.
Unknown identifier: 'Instantiate'.

Questions,

1) Why is it that "instance" cannot be accessed within a class? Isn't it supposed to be a public variable?
2) "Instantiate" function works in Start()/Update() root functions. Is there a way to make it work from within member functions?

Thanks in advance!

share|improve this question
add comment

1 Answer

You have a couple of things in your code backward. This is how it should be:

var thePrefab : GameObject;

class eg_class   {   
    function eg_func(){    
        var instance : GameObject;
        instance = Instantiate(thePrefab,transform.position,transform.rotation);
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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