Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Stufle · Apr 29, 2020 at 05:59 AM · updateonce

How to run a update function once

I have this code

 public float speed;
 // Start is called before the first frame update
 void update()
 {
     if (Score.score > 2)
     {
         speed = 4;
         KillPipes();
     }
     if(Score.score > 50)
     {
         speed = 5;
     }
     if(Score.score > 100)
     {
         speed = 6;
     }
 }

I only need this to run once, and then delete or disable the code, how can I do that?

Comment
Add comment · Show 4
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 MusapKahraman · Apr 29, 2020 at 06:17 AM 0
Share

Your code does not have an update function in it. The start function is only called once when the game runs. Isn't that what you want? To disable the code, just use that statement:

 enabled = false;
avatar image Stufle MusapKahraman · Apr 29, 2020 at 09:42 AM 0
Share

i was ment to be in a void update, I copied the wrong thing, but now it is fixed, does enabled = false, still is a code that could stop this?

avatar image MusapKahraman Stufle · Apr 29, 2020 at 11:00 PM 0
Share

Yes, it will disable the script and the update function will not be called unless you enable it again by another script.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Apr 29, 2020 at 07:01 AM

If you put the update function in a script and that script does not get destroyed after the first time it runs, it will continue. I think you are potentially looking for behaviour that can be provided by a coroutine:

 void Start(){
     StartCoroutine(OneUpdate());
 }
 
 IEnumerator OneUpdate(){
 
     yield return new WaitForEndFrame();
     
     //Your logic 
 }
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
0

Answer by FZ_Applications · Apr 29, 2020 at 07:35 AM

It is not clear what the question asks for.

The Start() method runs only once anyway.
If you want to destroy the script after execution you can write:

 void Start(){
      //Start Code
      Destroy(this); //Destroys this script
  }


If you want to destroy the gamObject after execution write:

 void Start() {
      //Start Code
      Destroy(gameObject);
 }
Comment
Add comment · Show 1 · 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 Stufle · Apr 29, 2020 at 09:41 AM 0
Share

i copied the wrong script, but now i changed it, it is supposed to say void update not void start. and I would like for it to not run after, because now when the score reaches 2, it destroys the pipes non stop, and I would like for it to destroy them once and never run again.

avatar image
0

Answer by MusapKahraman · Apr 29, 2020 at 11:08 PM

You can use enabled = false but it will disable the script and the update function will not be called unless you enable it again by another script.

     private void Update()
     {
         if (score > 2)
         {
             speed = 4;
             KillPipes();
             enabled = false;
         }
         // This code will not reach beyond this line 
         if (score > 50)
         {
             speed = 5;
         }
 
         if (score > 100)
         {
             speed = 6;
         }
     }
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

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.

Follow this Question

Answers Answers and Comments

130 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Call A Function At A Certain Time 1 Answer

Update Just Once 3 Answers

Play sound ONCE! 3 Answers

Run function once, is this method appropriate? 2 Answers

Calling a method once in update 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges