Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by littlehollow · Apr 28, 2019 at 02:20 PM · update functionbooleanslateupdate

Specific question + best practices for bools that are true for one frame only and need to be detected by other scripts

Hi, would appreciate some insight on this issue if anyone has any:

In my main game I have a selection system setup so the player can click different interactable objects and use them. I have two bools set up in the Selection script, isSelected which remains true for every frame that the object is selected for, until it's deselected, and selectStart which should just register true for the first frame it was selected, then change to false on the next frame. Both bools are public because other scripts need to detect them to do their thing.

The way I had it working so that selectStart is only true for the first frame is to detect if it's true in LateUpdate and then set it to false. This was working fine and was detectable from other scripts as well:

In Selectable script:

  public void SelectMe()
     {
         isSelected = true;
         selectionStart = true;
     }
 
    void LateUpdate()
     {
         if (selectionStart)
                   selectionStart = false; //so that wasSelected is only true for the first frame where it was selected

     }


In Secondary Script:

 void Update()
 {
      if (selectable.selectionStart)
      {
            print(gameObject.name + "'s selectionStart triggered"); 
            //do other stuff like play particle FX... 
      }
 }  

I then started a new project for Ludum Dare this weekend and borrowed a bunch of things from the original game, including the Selection script (with isSelected and selectStart) and another script that detects for these variables. Except in the new game, the second script doesn't detect selectStart successfully. The scripts are exactly the same, and no changes were made to script execution order. I then noticed that if I change the second script so it looks for selectable.selectionStart in LATE update instead of update, it does work. So I'm not sure what causes this exactly.

Also the followup question would be, is this a hackey way of doing things? What is the best practice way of having a "trigger type" bool, true for only one frame, be detectable in another script?

Thanks for any help!

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

1 Reply

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

Answer by Hellium · Apr 28, 2019 at 03:14 PM

Excuse me if the following suggestion does not fit your needs, I may not have all the information.


If I understand correctly, in the end, you want the secondary script to be warned when SelectMe is called right? You don't really needs the booleans, do you?


I would use events in your case and completely put aside booleans .

 // Selectable script

 public UnityEngine.Events.UnityEvent OnSelected;

  public void SelectMe()
  {
      if( OnSelected != null )
          OnSelected.Invoke();
  }


 // Secondary script

 private void Start()
 {
     selectable.OnSelected.AddListener( OnSelectableSelected ) ;
 }

 private void OnSelectableSelected ()
 {
     print(gameObject.name + "'s selectionStart triggered"); 
 }
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 littlehollow · May 06, 2019 at 04:56 PM 0
Share

Thank you so much, I had no idea about events. I thought the bool thing was tiresome but didn't know there was another way. This is going to change everything!

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

170 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 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

Why doesn't this boolean code work= 0 Answers

How do get multiple variables from different objects to test if they are true? 1 Answer

ScrollRect.LateUpdate Performance Spike Optimization Help 0 Answers

In updating some score text, why should we use the Update function instead of separate functions? 0 Answers

Stop Monobehaviour updates during LoadScene? 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