problem spawining script

hello all
i have problem now
that my script should spawn 2 kind of prefabs randomly
but that is never happened
why ?

As you can see there is 2 elements
and the output is just Road1
can i know why
and that is the spawn script !

using UnityEngine;
using System.Collections;

public class SpawnRoad : MonoBehaviour {
    public GameObject  []   obj  ;
    public float spawnmin = 1f   ;
    public float Spawnmax  = 2f  ;
    private bool x  ;
    // Use this for initialization

    void Awake ()
    {

        x = true ;
    }


    // Update is called once per frame
    // Use this for initialization
    void Update() {
        if(transform.position.z % 500 == 0 )Spawn () ;
           
    }

   
    void Spawn ()
    {

        Instantiate(obj[Random.Range(0,0)],transform.position, Quaternion.identity) ;
   
        //Invoke("Spawn",8 ) ;


       
       
    }
}


//
//using UnityEngine;
//using System.Collections;
//
//public class SpawnScript : MonoBehaviour {
//    public GameObject  []   obj  ;
//    public float spawnmin = 1f   ;
//    public float Spawnmax  = 2f  ;
//   
//   
//    // Use this for initialization
//    void Start () {
//        Spawn()  ;
//    }
//   
//    void Spawn ()
//    {
//        Instantiate(obj[Random.Range(0,obj.GetLength(0))],transform.position, Quaternion.identity) ;
//        Invoke("Spawn",Random.Range(spawnmin , Spawnmax )) ;
//       
//       
//    }
//}

void Spawn()
{
   Instantiate(obj[Random.Range(0,1)],transform.position, Quaternion.identity) ;
}

DemonHunter i think is otherwise right, but change Random.Range() arguments to 0 and 2

''Returns a random integer number between min [inclusive] and max [exclusive] (Read Only).

If max equals min, min will be returned. The returned value will never be max unless min equals max.‘’

1 Like

jaasso
thanks man

jasso
can you tell me
why ( 0,1)
didn’t work also ?
it is just work when i make it ( 0 , 2 ) ?

it’s because Random.Range, when using integers, will never return the maximum value you put in, it ranges from minimum to one below maximum

Random.Range(0, 2) will return 0 or 1

Random.Range(0, 3) will return 0, 1 or 2

Random.Range(0, 4) will return 0, 1, 2 or 3

and so on

1 Like

Thanks man