- Home /
calling function once?
Under function update its checking to see If the score has changed, and if it has then it will call/trigger/activate the "Spawn" function and Instantiate an object. The problem is that it Instantiate hundreds of objects, its calling it every frame. how can I get to only call spawn once.
#pragma strict
var Score : float = 5f;
var NPC : Transform;
var NPCPoint1 : Transform;
function Update(){
if (Score == 5){
Spawn();
}
}
function Spawn(){
Instantiate(NPC, NPCPoint1.position, NPCPoint1.rotation);
}
You start your Score variable at a value of 5. You never change this, so the condition in this line :
if (Score == 5){
is always true, so it will call your Spawn function every frame. A quick fix is to change the value of score inside this if statement :
if (Score == 5){
Spawn();
Score = 4f; //Or any other value that is not 5.
}
Answer by ahmedbenlakhdhar · Dec 07, 2014 at 10:16 PM
You may add a boolean variable to check if the function was called, like this:
#pragma strict
var Score : float = 5f;
var NPC : Transform;
var NPCPoint1 : Transform;
// This is the added boolean variable.
// It initially contains false because Spawn() is not called yet
var spawned : boolean = false;
function Update(){
// This condition does not get true value if spawned is true
if (Score == 5 && !spawned){
Spawn();
//Now spawned get true value because Spawn() is called
spawned = true;
}
}
function Spawn(){
Instantiate(NPC, NPCPoint1.position, NPCPoint1.rotation);
}
Answer by static_cast · Dec 07, 2014 at 10:25 PM
Instead of using booleans, you could implement two scores, with one that "catches up".
var score = 5;
var oldScore = 4;
function Update() {
if(score != oldScore)
{
Spawn();
oldScore = score;
}
}
This way he could reset the trigger without relying on Spawn() triggering. ;)
This is not as good any of the previous solutions posted... You seem to like posting repeat answers to peoples questions, which serves no productive purpose :(
Dude shut up, his anwser was the best in this case it brought an simple solution to the problem and was reasonabel. probally the best answer the person will get. and where is he copying? i dont see any solution here that is nearly as short.
Wow, mister. Hold your horses. First of all, your comment was the worst solution on here. Secondly, this is $$anonymous$$UCH $$anonymous$$UCH better than all of the other answers, as the guy said he was checking if the score changed, not if the score turned to five. $$anonymous$$aybe he wants multiples of five, but everybody else's scripts didn't show any way to reset.
$$anonymous$$ine was just a quick comment made before the question was published, and that is the point it is a comment, a push in the right direction, not an answer... You keep posting copycat answers on posts which is not a great thing to be doing!
Apologies.
When a question gets posted on here, everybody goes to it and answers what they think is the solution. Obviously many of the answers are going to be similar, as it's the same problem.
Look at the other two answers on this question. They're both the same, but they have different variable names. At least $$anonymous$$e was different. :|
Answer by Baste · Dec 07, 2014 at 10:22 PM
Just throw a trigger in there to turn off spawning once you've spawned one thing:
var hasSpawned : bool = false;
function Update(){
if (!hasSpawned && Score == 5){
Spawn();
hasSpawned = true;
}
}