- Home /
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
Answer by duck · Dec 03, 2009 at 09:55 AM
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)
Very correct, I'll remove my incorrect answer. I never really bothered to look too deeply into it as I'm not sure I've ever had to call Instantiate without it being a class inherited from Object.
Answer by btiger · Aug 16, 2010 at 06:59 AM
Or you can use this
GameObject.Instantiate(sourceObj, position, rotation);
$$anonymous$$aybe its the newer version of Unity now but this one worked for me whereas Object.Instantiate did not.
Same here. I am on unity 4 so it might be related, but Object.Instantiate leads to a "'Instanciate' is not a member of 'Object'" error, while using GameObject does work. Plus, it makes sense to use GameObject being that what we are trying to instanciate is indeed a GameObject.
Answer by piko · Jun 06, 2012 at 07:52 AM
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...
That transform error is nothing to do with the instance of Cannonball - it's to do with the object the code is running on - which I'm guessing isn't a $$anonymous$$onoBehaviour?
Well how can I tell if its a $$anonymous$$onoBehaviour?
I deleted the script from the object inside the scene--as to start fresh--and attempted to apply the script back onto the object. I can't! A message box pops up "Can't add script behaviour cannonBlast (name of my script). You need to fix all compile errors in all scripts first!
Its the same error: BCE0005: unknown identifier: 'transform', that it's telling me to fix...
Answer by unityart3ds · Dec 19, 2012 at 07:53 PM
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! :)(:
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
instantiate a prefab and getting error Unknown identifier: 'car'. 0 Answers
Instantiate coming up with errors for unknown reasons 1 Answer
Instantiating Prefab from Javascript - BCE0005: Unknown identifier: 'Prefab' 2 Answers
Setting the variable on a GameObject to be instantiated (not yet existent) cs and js 2 Answers