Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 pjcarey97 · Aug 09, 2012 at 08:50 PM · boolsimplefalsesecondstrue

How do I switch a bool every two seconds?

How do I make a bool switch between true and false every two seconds?

Comment
Add comment
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

3 Replies

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

Answer by code-blep · Aug 09, 2012 at 08:55 PM

Hi! this is my first answer so please be gentle with me people ;)

 function Update()
 {
 whatever = false;
 yield WaitForSeconds (2);
 whatever = true;
 }

Hope it helps :)

Paul

Comment
Add comment · Show 9 · 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 FLASHDENMARK · Aug 09, 2012 at 09:01 PM 1
Share

Congrats on that small milestone :)

Well two things.

  1. You are missing the opening and closing bracket after function Update. It should be like this: "function Update ()". but that doesn't really matter that much.

  2. And you are using yield in Update which is a function that cannot be a coroutine.

So you should create a new function and call that from the Update function. But even when doing so the script properly wouldn't work as expected anyway.

avatar image code-blep · Aug 09, 2012 at 09:04 PM 1
Share

Curse my eagerness to help. We will use my example on what NOT to do. Thank you for correcting me OrangeLightning. I'll up vote yours...

I've updated the brackets for what it is worth...

avatar image FLASHDENMARK · Aug 09, 2012 at 09:09 PM 0
Share

Wow, much appreciated. Do not worry we have all been there :)

avatar image Mander · Aug 09, 2012 at 09:13 PM 0
Share

function Update() { whatever = false; lefunction();

}

function lefunction(){ yield WaitForSeconds (2); whatever = true; yield WaitForSeconds (2); }

here

avatar image FLASHDENMARK · Aug 09, 2012 at 09:21 PM 0
Share

Ahh... That is pretty much the same thing. Although you do call a function that can be a coroutine, but this will not switch a bool every two seconds. It will wait two seconds and the it will be true forever.

Show more comments
avatar image
2

Answer by FLASHDENMARK · Aug 09, 2012 at 08:56 PM

 var thatBoolean : boolean;
 
 function Awake (){
     InvokeRepeating("Switch", 0, 2);
 }
 
 function Switch (){
     thatBoolean = !thatBoolean;
 }

There you go.

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 Lachee1 · Aug 10, 2012 at 12:16 AM

 var isToggling : boolean = false;
 var ToggleOnOff : boolean = false;
 function Update(){
  if (!isToggling){
    Toggle();
  }
 }
 
 function Toggle(){
  isToggling = true;
  ToggleOnOff = true;
   yield WaitforSeconds(2);
  ToggleOnOff = false;
   yield WaitforSeconds(2);
  isToggling = false;
 }


That will toggle it on, wait for 2 secs, toggle it off, wait 2 secs more, then loops

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

12 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

Related Questions

Why does this simple script not work????? 0 Answers

Set only one true 1 Answer

activating object only on true 1 Answer

C# bool setting itself to true 1 Answer

How can I make 2 objects that are close to each other being treated as being in the same position? 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