Javascript Classes and Instantiate

How can I instantiate a prefab within a javascript class?

This code works just fine

var Ball : Transform;
Instantiate(Ball, Vector3 (12, 12, 0), Quaternion.identity);

However if i put the same code in a dummy class

var Ball : Transform;    
class testing{
    function testing(){
         Instantiate(Ball, Vector3 (12, 12, 0), Quaternion.identity);
    }
}
var test = new testing();

i get this error.

BCE0005: Unknown identifier: 'Instantiate'.

Could someone please show me what im doing wrong please.

Thanks so much.

J

Instantiate is a method (another name for function) on Unity's Object class, but it's a static method, so it is actually available from everywhere. You don't have to inherit from Object to be able to use it. However, if your class doesn't inherit from Object, you need to specify the path to the function in full like this:

Object.Instantiate(sourceObj, position, rotation);

The confusion may be that if your class does inherit from Object, you don't need to use the Object. prefix in order to use the function.

(note - don't replace 'Object' with a variable, you actually have to use the word 'Object' as written)

Or you can use this

GameObject.Instantiate(sourceObj, position, rotation);

I believe it is the new version of Unity, because all of my scripts containing classes give me errors when I Instantiate or transform.(position, rotation, etc) anything involving variables from those classes. My entire test game is now a mess.

I was able to fix the Instantiate problem by adding GameObject. in-front of Instantiate…

cannon is a class name
cannonball is a variable declared in this class
spawnPos is a variable declared in this class
instanceCannonball is a variable declared as a GameObject outside of this class, like: var instanceCannonball : GameObject;

instanceCannonball = GameObject.Instantiate (cannon.cannonball, cannon.spawnPos.position, cannon.spawnPos.rotation);

The problem I still run into is doing anything with that instance, like, adding force–yes, it’s a rigid body. The code follows…

mechanics is a class name
force is a variable declared in this class

instanceCannonball.rigidbody.AddForce (transform.forward * mechanics.force);

This is where I get the error: BCE0005: unknown identifier: ‘transform’. I’m still searching unityAnswers before I ask anyone yet…

var ball : GameObject;

function Update () {if(Input.GetButton(“fire1”))

{Instantiate(ball, transform, rotation);}

}

Its been a little sence I did this but makeing the ball a Game Object will allow you to take it from the project and not just from the scene.

I hope this helps…

Send me your Email and Ill hooke you up with a free body form

My Email is unityart3ds@gmail.com

Thanks! :)(: