• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 to make the unity editor respond while a script is working? 2 Answers

Cant change variable speed in script 1 Answer

I'm wondering on how to decrease the value of variable 'maxForwardSpeed','maxSidewaysSpeed','maxBackwardsSpeed' inside the Class 'CharacterMotorMovement'. 1 Answer

index out of range problem with for loops and arrays. 1 Answer

How Can I Reset Values?(Unity3D) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges