Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Seabat10 · Aug 16, 2018 at 07:24 PM · unity 5variableloopfordecrease

Unity stop working after running this code, help!!!!!!!! (decremented of for varible)

unity stop work after i run this code: i used "i--;" before, but for some reason i don't work this time, pls help

 public GameObject[] Cards = new GameObject[3];
 public Transform[] Spawnpoint = new Transform[6];
 bool[] TakenPos = new bool[6];
 int Pos;


 void Start () {
     
     for (int i = 0; i <= 3; i++){
         
         for (int j = 0; j <= 2; j++){

             Pos = Random.Range (0,5);

             if (TakenPos [Pos] == false) {
                 Debug.Log ("Spawnpoint: " + Pos);
                 TakenPos [Pos] = true;
             } else {
                 Debug.Log ("Spawnpoint: OCUPADO");
                 j--;
             }

         }
     
     }

 }
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

3 Replies

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

Answer by Seabat10 · Aug 30, 2018 at 02:17 PM

i have solved this, i will show you the code: this throw random numbers without repeat ;) void RandomNumbers(){

     num[0] = Random.Range (0, num.Length); //El primer numero no hace falta compararlo

     for (int i = 1; i < num.Length; i++) { //Va tirar numeros aletorios dependiendo del array

         num [i] = Random.Range (0, num.Length); //Numero aleatorio
         check = false; //Resetea la variable

         while(check == false){ //checkea el boolean

             if (i != j) { //Si no se compara con sigo mismo

                 if (num [i] == num [j]) { //Si es igual a otro numero de array, tira otro numero

                     num [i] = Random.Range (0, num.Length);
                     j = 0; //Resetea el comparadro

                 } else { //Si es correcto, aumenta el comparador
                     j++;
                 }

             } else { //Si se compara con sigo mismo, salta a la siguiente
                 j++;
             }

             if (j == num.Length) { //Si se comparo con todos, resetea variable, quiebra el while y pasa a la siguiente
                 check = true;
                 j = 0;
             }

         }
     }

 }
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
avatar image
0

Answer by JVene · Aug 16, 2018 at 10:43 PM

If I assume that the TakenPos array starts out filled with false values (it is unless there is other code involved not posted).


This means that unless Pos never happens to generate the same value twice within about 5 tries (which is a very low probability), this would run forever. Put another way, all entries in TakenPos start out as false, which means any Pos value will satisfy the first test, setting the TakenPos[ Pos ] to true. However, there is a very good likelihood that Pos will end up being set to a value already set to true, at which point j is decremented. As the routine proceeds, however, the probability that Pos produces a value for which TakenPos[ Pos ] is already true approaches 1 (or 100%).


Put another way, it would be a very rare situation where this code doesn't create a TakenPos array that is all true, and thus defines an infinite loop. That is ensured by the outer loop in 'i', because even though j will be reset to zero, TakenPos will already be a list of 'true' bools, or so close to it that by the second or third pass through 'i', there's virtually no chance the first test will ever fire again, meaning this will eternally loop with j being decremented, then incremented, then decremented, then incremented - with no possible way out.


What you require is to decide what TakenPos should look like when correctly initialized. I assume that would be somewhere around 1/3 to no more than 2/3rds true entries, and the rest as false entries remaining.


A simple, single loop in j of 6 items would suffice, without decrementing j, and no outer loop in i, merely skipping any repeated Pos entries.


However, this is likely a waste of time. The theory of operation is that only Pos entry duplicates can hope to create non-true results in the result array of TakenPos.


It would be better and more straight forward to establish a volume that is randomly assigned between 2 and 4 (or some other extremes, I merely assume you'd want at least two true entries but no more than 4 entries). Then, loop that volume, creating the random Pos entries for that count, perhaps not even bothering to check if they are already true (that merely continues to randomize the ultimate volume of true entries).

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
avatar image
0

Answer by Seabat10 · Aug 19, 2018 at 12:06 AM

Thank you so much!!! I could'nt solve my problem but now i understand why it does'nt work that's what i want to do: i have 6 cards, 3 pairs on different places (static spawnpoints), i just need to random the spawnpoints without repeating them. @JVene

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

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

How do i render a Mesh loop? 1 Answer

Problem with For Loop for Text objects,Why isn't this for loop for a list of text objects not working? 0 Answers

How Can I Reset Values?(Unity3D) 0 Answers

How to make the unity editor respond while a script is working? 2 Answers

Problem with decrement 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