Question by 
               danmoople · Apr 18, 2017 at 01:19 PM · 
                unity 5scripting problemunityeditorunity 4.6  
              
 
              Why IF doesn't work with Instantiate?
I created a scene when a random integer is generated then it chooses random object and should spawn it. But it doesn't happen. Why? If I delete if-statement - it works well. using UnityEngine; using System.Collections;
 public class SpawnLeftEnvironment : MonoBehaviour {
 
     public GameObject environment1;
     public GameObject environment2;
     public GameObject environment3;
     public GameObject environment4;
 
     private float counter = 23.5f;
     private float startX1 = 6.53f;
     private float startX2 = 37.61f;
 
     void Start () {
         InvokeRepeating("SpawnLeftEnv", 0.0f, 1.0f);
     }
 
     void SpawnLeftEnv(){
 
         int randomInteger = Random.Range(0,3);
         
         if(randomInteger == 3){
             Instantiate(environment3, new Vector3(startX1 + counter, 21.42f, -6.92f), Quaternion.Euler(0,0,0));
         }
 
         else if(randomInteger == 4){
             Instantiate(environment4, new Vector3(startX2 + counter, 21.34f, -6.85f), Quaternion.Euler(0,0,0));
         }
         
         counter += 23.5f;
     }
     
     void Update () {
     
     }
 }
 
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by tanoshimi · Apr 18, 2017 at 01:21 PM
 int randomInteger = Random.Range(0,3);
 
               generates a number that is either 0, 1, or 2.
So, neither
 if(randomInteger == 3)
 
               or
 if(randomInteger == 4)
 
               will ever be true...
Your answer
 
             Follow this Question
Related Questions
How to assign GameObject to Script? 1 Answer
How to create a script to control a path like in game 'Tbe Witnsss' 0 Answers
How to run a function after every X minutes in Unity Script? 1 Answer
How to assign Instantiated object through script? 1 Answer
Playerprefs are not deleting when an apk is uninstalled? 0 Answers