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
0
Question by Blahfargl · Oct 26, 2014 at 04:04 PM · c#updatetimerfor-loop

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);
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · 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 Blahfargl · Oct 26, 2014 at 05:22 PM 0
Share

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.

avatar image Habitablaba · Oct 26, 2014 at 05:57 PM 0
Share

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.

avatar image Blahfargl · Oct 27, 2014 at 08:00 AM 1
Share

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

avatar image
0

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 :)

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

Making a fill take exactly n seconds to complete 2 Answers

How to integrate timer into my C# script? 2 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