- Home /
How can I create a prefab when an object is pressed
Hi, I have a 3d button that I want to create a player with but for some reason I get this error Assets/Standard Assets/PERSONAL/Player/create redP.js(3,21): BCE0077: It is not possible to invoke an expression of type 'UnityEngine.GameObject'.
and this is the code I have.
var thePrefab : GameObject; function OnMouseDown () { Instantiate(thePrefab(0,11,0));
}
Is this because of the type of function I have?
Hi, first, organize your code bro, don't put your functions in the same line of the variables and etc, it should look like this:
var thePrefab : GameObject;
function On$$anonymous$$ouseDown(){
Instantiate(thePrefab(transform.position));
}
So, where have you defined what is the prefab? normally we would set it a public variable and just drag and drop thePrefab on the Unit game object that the script is attached. If you did this, it should works ok. And another thing, from where those numbers comes from? (0,11,0). Don't hard code the position like this, you can connect with the transform of the button game object, and it will have a position variable, use that for instantiate your player..
thankyou so much it worked and by the way the text box messed up my code.
Answer by create3dgames · Jul 01, 2013 at 07:48 PM
This is good code:
public var thePrefab : GameObject;
public var spawnPos : Vector3;
function OnMouseDown(){
Instantiate(thePrefab, spawnPos, transform.rotation);
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Calling a function after game over to retry 0 Answers
Object will not instantiate 1 Answer
How to access variable from another function? 2 Answers
How can I make a function wait before it starts running again? 1 Answer