- Home /
Question by
Novadev · Apr 15, 2015 at 01:04 PM ·
javascriptbooleaninstantiating
How to instantiate only once with boolean
Hello good people,
I'm new to booleans (and scripting for that matter) and i've run into a problem. I want a prefab to instantiate OnKeyDown, but after that I want that key to go dead, so it only instatiates the prefab once. The script below doesn't actually instantiate anything.
I think it has something to do with line 19. How do I prevent another instantiation OnKeyDown after it has completed?
Thanks!
#pragma strict
var tilePrefab : GameObject;
var canCreate : boolean;
function Start ()
{
canCreate = true;
}
function Update ()
{
if(canCreate == true)
{
if(Input.GetKeyDown("c"))
var instance : GameObject = Instantiate(tilePrefab);
{
canCreate = false;
}
}
}
Comment
Best Answer
Answer by YoungDeveloper · Apr 15, 2015 at 01:06 PM
Try this
var tilePrefab : GameObject;
private var canCreate : boolean = true;
function Update (){
if(canCreate){
if(Input.GetKeyDown(KeyCode.C)){
canCreate = false;
var instance : GameObject = Instantiate(tilePrefab);
}
}
}
Just a heads up, your boolean canCreate was public, most likely false by default, that's why it never launched.
Your answer
