• 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
Question by FK22 · Mar 11, 2019 at 12:06 AM · activeupdate functionframesonenableondisable

How to add only 1 value in a list in the Update function when a GameObject is active for many frames

Hi all, I need some advises. In my C# script, I get the active gameobjects during play mode. Those gameObjects that are active are:

1- First, I get the ones that the application has selected randomly 2- and then after the first ones get disabled (setActive(false)), I get the selected gameObjects which are those that the user has pressed using their button.

So basically the application is: The user hears a sound being played (here I set active the gameObject that has that sound) and then after some frames, the sound gets disabled and then the user needs to press a button in screen to make a choice depending on the sound (here I set active the gameObject that has the button) and then I disable that game object after some frames.

Now every time one of those gameObjects gets selected, I try to add to my list some sort of value (lets say 1 in this case). But what happens is that I only manage to add the value in the Update() function but in this function I do not get only 1 value added in the list every time I get the first gameObject selected but because it updates every frame, this value gets added for like 125 times in the list until this gameObject gets diselected and the next gameObject (the one that holds the button) get active.

Ideally, I would want to:

1- Hear the sound (gameObject is active) => add only 1 value in list until the gameObject is deselected. 2- Then do the same thing for the gameObjects that hold the button. So when that gets selected = > add only one value in the other list until this gameObject is deselected. Then do the same process 72 times (because the number of trials in this application is 72).

Can anyone give me any advice how to achieve this?

Thanks,

Fjorda ,Hi all, I need some advises. In my C# script, I get the active gameobjects during play mode. Those gameObjects that are active are:

1- First, I get the ones that the application has selected randomly 2- and then after the first ones get disabled (setActive(false)), I get the selected gameObjects which are those that the user has pressed using their button.

So basically the application is: The user hears a sound being played (here I set active the gameObject that has that sound) and then after some frames, the sound gets disabled and then the user needs to press a button in screen to make a choice depending on the sound (here I set active the gameObject that has the button) and then I disable that game object after some frames.

Now every time one of those gameObjects gets selected, I try to add to my list some sort of value (lets say 1 in this case). But what happens is that I only manage to add the value in the Update() function but in this function I do not get only 1 value added in the list every time I get the first gameObject selected but because it updates every frame, this value gets added for like 125 times in the list until this gameObject gets diselected and the next gameObject (the one that holds the button) get active.

Ideally, I would want to:

1- Hear the sound (gameObject is active) => add only 1 value in list until the gameObject is deselected. 2- Then do the same thing for the gameObjects that hold the button. So when that gets selected = > add only one value in the other list until this gameObject is deselected. Then do the same process 72 times (because the number of trials in this application is 72).

Can anyone give me any advice how to achieve this?

Thanks,

Fjorda

Comment

People who like this

0 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 mchts · Mar 11, 2019 at 08:24 AM 0
Share

You could simply check if your gameObjet was already added to list

 ...
 if(![yourlist].Contains(gameObject)){
      [yourlist].Add(gameObject); 
 } 



0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

101 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

Related Questions

Object created in CustomEditor is leaked into scene when play is pressed. 0 Answers

delayed function problem 1 Answer

Finding the exact time in a WaitForSeconds that it is currently at 2 Answers

would Unity disable gameobject temporary and automatically when loading a big assetbundle? 0 Answers

OnSceneChange for Don'tDestroyOnLoad GameObjects? 0 Answers


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