Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Random.Next() Not Working? What am I doing wrong?

Discussion in 'Scripting' started by fryedrycestyle, Jun 26, 2013.

Thread Status:
Not open for further replies.
  1. fryedrycestyle

    fryedrycestyle

    Joined:
    Jun 8, 2012
    Posts:
    19
    Hey guys. I'm trying to randomly spawn enemies on the screen. To do so I'm using System.Random's Next() method.

    Basically what's happening is that whenever I start up the game in the editor the enemies always spawn in the same position (for me it's x: 850, y and z: 0). I want the enemies to spawn in different positions. I know creating an instance of System.Random() in the for loop can cause issues like this, but I have my System.Random() instance created in the Start() method. How can I get this to work as intended? Or is it something other than the Random() that might not be working? Obviously there's some other issues that this code might present, but right now I just want my enemies not all in the same position.

    Thanks a ton! Code's below.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. [System.Serializable]
    8. public class Enemy {
    9.     public Transform eType;
    10.     public int maxOnScreen;
    11.     [HideInInspector]
    12.     public int onScreen;
    13.    
    14.     public Enemy(){
    15.          
    16.     }
    17. };
    18.  
    19. public class SpawnController : MonoBehaviour {
    20.    
    21.     public List<Enemy> enemies;
    22.     tk2dCamera cam;
    23.    
    24.     System.Random rangeCalc;
    25.    
    26.     // Use this for initialization
    27.     void Start () {
    28.         cam = tk2dCamera.inst;
    29.         rangeCalc = new System.Random();
    30.     }
    31.    
    32.     // Update is called once per frame
    33.     void Update () {
    34.         foreach(Enemy enemy in enemies)
    35.         {
    36.             EnemyController eScript = enemy.eType.GetComponent<EnemyController>();
    37.            
    38.             if (enemy.onScreen < enemy.maxOnScreen
    39.                  WhaleController.distance >= eScript.distanceStart)
    40.             {
    41.                 Debug.Log("Spawn!");
    42.                 spawn ();
    43.             }
    44.         }
    45.     }
    46.    
    47.     void spawn() {
    48.        
    49.         foreach (Enemy enemy in enemies)
    50.         {
    51.             EnemyController eScript = enemy.eType.GetComponent<EnemyController>();
    52.             if (WhaleController.distance >= eScript.distanceStart)
    53.             {
    54.                 for (int i = 0; i < enemy.maxOnScreen; i++)
    55.                 {      
    56.                     int ePosX = rangeCalc.Next(eScript.minX, eScript.maxX);
    57.                     ePosX += (int) cam.ScreenExtents.xMax;
    58.                     int ePosY = rangeCalc.Next(eScript.minY, eScript.maxY);
    59.                     if (enemy.onScreen < enemy.maxOnScreen)
    60.                     {
    61.                         Transform newEnemy = (Transform) Instantiate(enemy.eType, new Vector3(ePosX, ePosY, enemy.eType.position.z), enemy.eType.localRotation);
    62.                         newEnemy.parent = this.gameObject.transform;
    63.                         enemy.onScreen++;
    64.                     }
    65.                 }
    66.             }
    67.         }  
    68.     }
    69.    
    70. }
    71.  
     
  2. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Why not just use Random.Range (Unity's built in Random) ?
     
  3. fryedrycestyle

    fryedrycestyle

    Joined:
    Jun 8, 2012
    Posts:
    19
    I tried that, but it didn't produce any different results. =S
    I figure that's because it's a random number generator, and they don't tend to work well in for... loops, unless instantiated outside the loop which you can't appear to do with the UnityEngine.Random class.

    Someone correct me if I'm wrong though!
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There's nothing special about random number generators, including Random.Range, that would prevent them from working in for loops. There's no difference at all between using Random.Range in a loop or anywhere else.

    --Eric
     
  5. fryedrycestyle

    fryedrycestyle

    Joined:
    Jun 8, 2012
    Posts:
    19
  6. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    One possible problem. new Random() uses a default seed. I know it depends on system's time but I don't know what is the precision (seed is an int32). It is possible that you maybe getting the same seed? Try to use a second Random constructor that lets you specify a seed. Maybe try to use GetHashCode of the current object as a seed?
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Just use Random.Range, and don't set the seed.

    --Eric
     
  8. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Code (CSharp):
    1.  
    2. for (int i = 0; i < Slot.Length; i++)
    3. Slot[i].Quantity = Random.Range(0,100);
    4.  
    Gives the value of 23 or 86 for each number.
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Nope. (As discussed in your other thread; there wasn't really any need to necro this one.)

    --Eric
     
Thread Status:
Not open for further replies.