- Home /
For loop resetting itself, but needs to stop
Hi, using C#, my problem is when my code runs I'm supposed to turn three random objects from an array of objects called 'distractors' every third second, but somehow it turns a lot more objects. The first for loop is run multiple times instead of just the one time (about 35 times). It is supposed to go from 0 to two and then stop, but it just starts over.
I honostly do not see why this problem is happening, so any help is appreciated. Or if you have a better way to turn three random objects from a list of objects that would also be good.
void Update () {
timeLeft -= Time.deltaTime;
if(timeLeft < 0)
{
for(int k = 0; k < 3; k++)
{
tmp[k] = Random.Range(0, distractors.Length);
Debug.Log(tmp[k] + " start | k: " + k);
for(int j = 1; j < k; j++)
{
if(k > 0)
{
if(tmp[k] == tmp[k-j])
{
tmp[k] = Random.Range(0, distractors.Length);
//Debug.Log(tmp[k] + " new | j:" + j);
j = 0;
}
}
}
//Debug.Log(tmp[k]);
distractors[tmp[k]].transform.Rotate(Vector3.forward * z);
}
timeLeft = 3.0f;
}
//Debug.Log(timeLeft);
}
Answer by Habitablaba · Oct 26, 2014 at 04:35 PM
If you're only doing 3 objects, it makes complete sense to just brute force it.
Declare 3 variables instead of one and do the work 3 time. Just remove the loops completely. You're not doing so many things that it becomes cumbersome to do it manually.
I actually tried that and it was the same problem. I made three integers with random value assigned and rotated the three objects in the array with the corresponding value. This also ran multiple times during the if statement of timeLeft < 0.
Sure, that makes complete sense. Update is called once per frame, so it will check your if statement each time. Every frame where timeLeft is not 0 will result in your code getting run -- the for loop version or the manual version.
If you only want your code to run once, you need to add a flag that you can toggle. If the distractors code has run, don't run it again. Or you can set up another timer if you want it to happen more than once, but less than every frame.
Ok so the problem was i was applying this code to all the objects, as they are clones of each other, so it was run as many times as i have objects. Placed the code elsewhere and it was. Thanks for the help
Answer by Deadcow_ · Oct 27, 2014 at 09:19 AM
"Calling every third second" is a great definition for a Coroutine :) for a start:
IEnunmerator DistractedLogic()
{
yield return new WaitForSeconds(3);
// your logic here
}
and launch it at start function StartCoroutine(DistractedLogic());
So, get three unique random values from an array is complex task. For a start you may get three unique random indexes
List<int> randomIndexes = new List<int>();
int length = myCollection.Length;
int count = 3;
while (randomIndexes.Count < count)
{
int random = Random.Range(0, length -1);
if (!randomIndexes.Contains(random))
randomIndexes.Add(random);
}
may be not the best approach, but it'll did the trick.
Second step: get objects by this indexes
List<MyObjects> myObjects = new List<MyObjects>();
foreach(int i in randomIndexes)
{
myObjects.Add(myCollection[i]);
}
okay. If you have collection myCollection
this code will give you myObjects
collection with three random objects from myCollection
.
Hope this'll help And excuse me for my terrible english :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613170008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Is an object with an update function possible? (C#) 1 Answer
Distribute terrain in zones 3 Answers