- Home /
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
You could simply check if your gameObjet was already added to list
...
if(![yourlist].Contains(gameObject)){
[yourlist].Add(gameObject);
}
Your answer
Follow this Question
Related Questions
would Unity disable gameobject temporary and automatically when loading a big assetbundle? 0 Answers
OnSceneChange for Don'tDestroyOnLoad GameObjects? 0 Answers
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