• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by ObviouslyInsane · Aug 10, 2011 at 07:12 PM · updatefunctiontimeronce

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ObviouslyInsane · Aug 11, 2011 at 12:26 AM 0
Share

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?

avatar image Meltdown · Aug 11, 2011 at 04:33 AM 1
Share

Have you tried putting the Destroy command within the curly braces where you check if(timeLeft

avatar image Eric5h5 · Aug 11, 2011 at 06:39 AM 0
Share

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.

6 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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);
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
10

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Noah-1 · Apr 20, 2012 at 09:25 PM 0
Share

Exellent solution my friend!!

avatar image mohammad-alavi-74 · Mar 27, 2016 at 07:15 PM 0
Share

really helped! like!

avatar image VRP360 · Apr 16, 2019 at 07:15 AM 0
Share

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!

avatar image highpockets VRP360 · Apr 16, 2019 at 07:46 AM 0
Share

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

avatar image
5

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 !.
     }
 
 }
 


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thegreatkoala · Aug 18, 2018 at 05:08 PM 0
Share

Thank you for that answer, it was a great help for me :)

avatar image Ajparker · Nov 16, 2020 at 12:46 PM 0
Share

THANK YOU !!

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Aug 11, 2011 at 06:38 AM 0
Share

Every frame is as often as the computer can manage, not every second. Unless you're only getting 1 fps.

avatar image DGArtistsInc · Aug 15, 2011 at 01:53 PM 0
Share

yeah your right thats what i meant

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Run function once, is this method appropriate? 2 Answers

Call A Function At A Certain Time 1 Answer

Loop a function, once per second, X number of times 1 Answer

Each update runs my code 6 times per line. Is it a bug? 1 Answer

Aiming with function "Update" not working 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges