- Home /
Limit click spawns to One
how would i limit a mouseclick to 1 so that functions will not repeat with a gajillion mouseclicks? for example, if a player clicks once, a beachball comes onto screen. unfortunately, if you keep clicking, more beachballs come, but there should only ever be the first one. once it's destroyed, a new one can be spawned. any help is appreciated. thanks!
Answer by Jessy · Jan 01, 2011 at 07:33 AM
var beachBallPrefab : GameObject;
private var beachBall;
function Update () { if (!beachBall && Input.GetMouseButtonDown(0)) beachBall = Instantiate(beachBallPrefab); }
"!beachBall" means there isn't a beach ball in the scene, either because you didn't make one yet, or because you destroyed it.
it was right in front of me hahaha. i love how difficult these simple things can be for new developers like myself. thanks to you and @Yoshie$$anonymous$$aster for the answers.
Answer by YoshieMaster · Jan 01, 2011 at 07:27 AM
Add a tag such as BeachballTag and then before instantiating the prefab check the following if statement:
JS:
if(FindGameObjectsWithTag("BeachballTag").length=0)
This can work but is slow. Also, there's no reason to tag it "BeachballTag". A tag is a tag. "Beachball" could be appropriate.