- Home /
Only run the function once
I have a script (See Below)
#pragma strict
var SpawnArray : GameObject[];
var roundedTime : int = 0;
var difficulty : float = 1.0;
var gameTime : float = 0.0;
var difficultyMod : int = 20;
var lowBlocks : GameObject[];
function Start ()
{
}
function Update ()
{
if(roundedTime % 5 == 0)
{
SpawnNewOBJ();
}
gameTime += Time.deltaTime * difficulty;
roundedTime = Mathf.Round(gameTime);
if(gameTime > difficultyMod)
{
difficulty += 0.5f;
difficultyMod *= 2;
}
}
function SpawnNewOBJ()
{
var rand : GameObject = SpawnArray[Random.Range(0, SpawnArray.Length)];
var randBlock : GameObject = lowBlocks[Random.Range(0, lowBlocks.Length)];
var inst = Instantiate(randBlock, new Vector2(rand.gameObject.transform.position.x, rand.gameObject.transform.position.y), Quaternion.identity);
}
What I am trying to do is when the rounded time is a divisible of 5, it spawns 1 object. But what is happening is it runs the function"SpawnNewOBJ" a million times because the rounded time stays divisible by 5 for 1 second. May be hard to understand sorry. How would I make it so the function only gets called once?
Answer by BiG · Jul 21, 2015 at 07:42 AM
The simplest thing I can think about is using a lock.
declare it as an exposed variable:
var lock : boolean = false;
Set as true when you're spawning:
function SpawnNewOBJ(){
lock = true;
//The rest of your code as is...
}
Remove it when new reassignment takes place:
roundedTime = Mathf.Round(gameTime);
lock = false;
Add it as a condition for the function's call:
if(roundedTime % 5 == 0 && !lock)
Hope this helps.
I think your solution while pose the same problem because you unlock your lock in the update function. because of the "Round" function assign the variable doesn't mean change his value.
(I haven't try it's just a guess)
Answer by Veldars · Jul 21, 2015 at 07:45 AM
For me the easiest way to do that is simply to make something like that :
if(roundedTime % 6 == 0)
{
SpawnNewOBJ();
gameTime += 1;
}
Or to retake the previous answer :
var lock : bool = false;
var prevTime : int = 0;
function Update ()
{
if(roundedTime % 5 == 0 && !lock)
{
lock = true;
SpawnNewOBJ();
}
prevTime = roundedTime ;
gameTime += Time.deltaTime * difficulty;
roundedTime = Mathf.Round(gameTime);
if (prevTime != roundedTime )
lock = false;
if(gameTime > difficultyMod)
{
difficulty += 0.5f;
difficultyMod *= 2;
}
}
function SpawnNewOBJ()
{
var rand : GameObject = SpawnArray[Random.Range(0, SpawnArray.Length)];
var randBlock : GameObject = lowBlocks[Random.Range(0, lowBlocks.Length)];
var inst = Instantiate(randBlock, new Vector2(rand.gameObject.transform.position.x, rand.gameObject.transform.position.y), Quaternion.identity);
}
Hope this help...
What if I made a timer and when its zero, I spawn an object. At the end of the spawn function, it resets the timer.
@Veldars ps. thanks for the reply
Your answer
Follow this Question
Related Questions
Not updating when script value is changed by an editor script 1 Answer
Variables in GUI not updating/changing? (javascript) 1 Answer
Can't Get 2D Sprite to Animate 0 Answers
Getting updated variables from another script 2 Answers
Script is causing immense lag, and I don't know what's causing it. 2 Answers