- Home /
Calling a function only once in Update
Hi everyone, I'm having an inssue with my script here:
var barDisplay : float = 3;
var timeLeft : float = .5; //amount of time on timer
static var finishTimer = false; //i made this global so you can access from other scripts
static var timerStarted = false;
function OnTriggerEnter (col : Collider){
if(col.name == "Player")
GameObject.Find("DoubleRockets").GetComponent("gunscript");
gunscript.initialSpeed += 200;
timerStarted = true;
}
function Update() {
if (timerStarted == true)
timeLeft -= Time.deltaTime;
if (timeLeft <= 0.0f)
{
GameObject.Find("DoubleRockets").GetComponent("gunscript");
gunscript.initialSpeed -= 200;
Destroy(gameObject);
}
}
The problem is that the gunscript.initialSpeed -= 200; part is being called more than once before the gameobject is destroyed. How can I make sure this function gets called only once?
Thanks for all the help guys, I think I'm getting there. It only calls the function once now but I have a new problem. The gameobject is being destroyed before the timer even has a chance to countdown. Here's my revised code:
var barDisplay : float = 3;
var timeLeft : float = 10; //amount of time on timer
static var gunSpeedReduced = false;
static var finishTimer = false; //i made this global so you can access from other scripts
static var timerStarted = false;
function OnTriggerEnter (col : Collider) {
if(col.name == "Player")
GameObject.Find("DoubleRockets").GetComponent("gunscript");
gunscript.initialSpeed += 200;
timerStarted = true;
}
function Update() {
if (timerStarted == true)
timeLeft -= Time.deltaTime;
if (timerStarted == true && timeLeft <= 0.0f)
Activate();
}
function Activate() {
if(timeLeft <= 0.0f && gunSpeedReduced == false)
{
GameObject.Find("DoubleRockets").GetComponent("gunscript");
gunscript.initialSpeed -= 200;
gunSpeedReduced = true;
}
Destroy(gameObject);
}
Can I get just a little more insight to what I'm doing wrong here?
Have you tried putting the Destroy command within the curly braces where you check if(timeLeft
You really shouldn't be using Update for this. Update is for things that run every frame. It's much simpler to use Invoke, coroutines, etc.
Answer by Meltdown · Aug 10, 2011 at 09:10 PM
static var gunSpeedReduced = false;
if (timeLeft <= 0.0f)
{
GameObject.Find("DoubleRockets").GetComponent("gunscript");
if(gunSpeedReduced == false)
{
gunscript.initialSpeed -= 200;
gunSpeedReduced = true;
}
Destroy(gameObject);
}
Answer by 4illeen · Aug 10, 2011 at 08:23 PM
You could always create a boolean variable equal to false at first and make an if statement which will check if it's false. Inside the if statement call your function and make the boolean equal to true
But this way, it will never be false again, or am I wrong? How could you make the boolean variable false again? Thanks in advance!
I think the idea of the original poster was to just call the function once, so he/she wouldn’t need to make it false again, but of course there is a way to make it false again, though. You would have to have another condition to be met to make it true. For example x amount of seconds go by, an object rotates 360 degrees about the y axis, transform.position == endPos, etc.. It just depends on your case, if you explain what you want to do, I can provide a solution. But if you want to do the same as the original poster (call a function just once inside update) than you never need to change the Boolean back to true. Personally, I would not make the Boolean at all and I would just start a coroutine when the trigger is entered, and call the function after yield return new WaitForSeconds(secondsToWait); is finished
Answer by Ali_unity · May 31, 2018 at 07:54 AM
bool MyFunctionCalled = false;
void Update()
{
If(MyFunctionCalled==false)
{
MyFunctionCalled = true;
Do Your Stuff Here And It Will Be Only Done Once !.
}
}
Thank you for that answer, it was a great help for me :)
U r the messenger of god. U just blow my $$anonymous$$d with this logic. Thank you so much.
Answer by DGArtistsInc · Aug 10, 2011 at 08:15 PM
If you want to call the function only once then create your own function or use function start. Anything under the Function Update is called every frame. Which is every second.
Every frame is as often as the computer can manage, not every second. Unless you're only getting 1 fps.
Answer by Atrius · Apr 21, 2012 at 05:43 AM
You should simply be using Invoke() for something like this. Pass it the function to call and the time period and it'll do the rest for you. You can call this on your component or whatever object you need.
No need to track the time and things manually unless you need to track it in increments, and even then if you need increments use InvokeRepeating() since it's easily cancelled.
Your answer
Follow this Question
Related Questions
Run function once, is this method appropriate? 2 Answers
Calling a method once in update 1 Answer
Call A Function At A Certain Time 1 Answer
Ensure that function only gets called once in a frame 2 Answers