Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Generating Multiple Different Random Numbers At Start

Discussion in 'Scripting' started by cristicristi8160, May 7, 2016.

  1. cristicristi8160

    cristicristi8160

    Joined:
    May 7, 2016
    Posts:
    17
    Hello!

    I have around 10 prefab objects and I want their X position to be random. It's a game where you have to avoid them so I try to change their X position in the Start function, but they change their position in the same time so rand.Next(-1,2) will generate the same value for all of them, and all of them (prefabs) have the same X position. What can I do so they have different X position?

    This is my code:

    using UnityEngine;
    using System.Collections;

    public class GM2enemyStart : MonoBehaviour
    {

    void Start()
    {
    System.Random rand = new System.Random();
    float px = rand.Next(-1, 2);
    float py = 28.809f;
    transform.position = new Vector3(px, py, transform.position.z);
    }

    }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Code (csharp):
    1. Vector3[] Spawns;
    2.     public GameObject[] Zombies;
    3.  
    4.     void Awake()
    5.     {
    6.         Spawns = new Vector3[Zombies.Length];
    7.  
    8.         for (int i = 0; i < Spawns.Length; i++) {
    9.             Spawns [i] = new Vector3 (Random.Range (-300, 300), 1, Random.Range (-300, 300));
    10.         }
    11.     }
    12.  
    13.     void Start ()
    14.     {
    15.         Spawn ();
    16.     }
    17.  
    18.     public void Spawn(bool randomAmount = true)
    19.     {
    20.         if (randomAmount) {
    21.             if (Zombies.Length > 0) {
    22.                 for (int zombie = 0; zombie < Random.Range (1, Zombies.Length); zombie++) {
    23.                     GameObject z;
    24.                     z = Instantiate (Zombies [zombie], Spawns [zombie], Quaternion.identity) as GameObject;
    25.                     z.transform.SetParent (transform);
    26.                 }
    27.             }
    28.         }
    29.     }
    so it's really simple, what my script does is create a Vector3 array of Random spawn positions based on how much prefabs you have (10), so it will create 10 random spawn points in the Awake Function.

    What the Spawn() function does is spawns a random amount of zombies (or whatever it is your dealing with) as a child of the GameObject the script is attached to (for example: ZombieManager, all zombies will be spawned as a child of this object) and Instantiates each of them at a its random position chosen in the Awake function
     
    Last edited: May 7, 2016
    Kacpair likes this.