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
1
Question by Alienjesus · Apr 24, 2011 at 04:12 PM · booleanswitch

On boolean becoming true, do something - how do I stop code activating over and over?

I'm trying to activate some code in the Update function to pick up an object when a boolean becomes true. However, the code at the minute continuously activates whilst the boolean is true, which causes the character to rapidly pick up and drop the object. Is there a way I could set it only to activate upon the boolean becoming true, and then not to activate again until the boolean becomes false, and true again? I would prefer to do it in the update function, otherwise a large chunk of code will have to change to work with GetComponents and SendMessages and the likes.

Thanks for your help!

Here is the script, although I'm not sure it's necessary for this:

if (zButton) { if (!hasObject) { //if the fiducial cant be seen, stop this script from picking up objects if (!renderer.enabled) { return; } if (Physics.Raycast(camera.main.transform.position, (transform.position-camera.main.transform.position), hit, 75, LayerMask)) { otherThing = hit.transform; if (otherThing.tag =="Moveable") { otherThing.rigidbody.isKinematic = true; otherThing.rigidbody.detectCollisions = false; otherThing.transform.position.z = 0; otherThing.parent = transform; otherThing.Find("Glowblock").light.enabled = true; hasObject = true; } }else { if (otherThing.tag == "Moveable") { otherThing.rigidbody.isKinematic = false; otherThing.rigidbody.detectCollisions = true;

                          transform.DetachChildren();
              otherThing.transform.position.z = 0;
              otherThing.Find("Glowblock").light.enabled = false;
              hasObject = false;
   //etc etc....

Comment
Add comment · Show 1
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 Uzquiano · Apr 24, 2011 at 04:22 PM 0
Share

Josuha's answer is a very good trick ;)

1 Reply

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

Answer by Joshua · Apr 24, 2011 at 04:18 PM

Ah, good question and it's important to do this as much as you can when scripting - to releive stress from the machine.

Your script now looks like:

var switch : Boolean;

function Update () {

 if(switch) {do stuff;}

}

you want to have the script know if the boolean is true (or false) and if it has changed from the previous frame. So add a var that holds the old value of your bool:

var switch : Boolean; var oldSwitch : Boolean;

function Update () {

 if(switch && switch == !oldSwitch) {do stuff; oldSwitch = switch;}

}

not that in the if function you turn the oldswitch var to hold the same value as the original boolean, so it won't be triggered again.

Cheers!

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 Alienjesus · Apr 24, 2011 at 04:38 PM 0
Share

This answer is brilliant, and worked great, thanks. I have a few other places where I could make use of this.

I just stuck in an extra bit of code which makes oldSwitch become false when switch becomes false, and now i can call the script over and over but only when the boolean becomes true. Thanks!

avatar image vincentdhlee · Jun 05, 2018 at 08:07 AM 0
Share

what is &&?

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

1 Person is following this question.

avatar image

Related Questions

Controlling multiple booleans 1 Answer

Switch code from Js to C# 1 Answer

switching the active boolean 1 Answer

Boolean check not referenced correctly from other script 2 Answers

Changing Missile Speed 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