Joash Mathew
Joash Mathew

Reputation: 3

The Same Object is Getting Instantiated again and again in C#

I'm a beginner at coding. I've been trying to recreate the popular mobile game 'Crossy Road' for a few days now. While I was trying to create a Level Generation script, I ran into a problem. The problem that I am facing is that when I try to Instantiate 3 different objects randomly, (in this case Grass, Road and Water) only grass gets generated. I would appreciate it very much if someone could tell me why it keeps instantiating the 'Grass' object repeatedly. I am recreating the game in Unity 5 fyi. The code is as follows -

using UnityEngine;
using System.Collections;

public class LevelGenerationScript : MonoBehaviour {

public GameObject Water;
public GameObject Road;
public GameObject Grass;
int firstRand;
int secondRand;
int distPlayer = 10;

Vector3 intPos = new Vector3(0,0,0);

void Update () 
{
    if (Input.GetButtonDown("up")) 
    {
        firstRand = Random.Range(1,4);
        if(firstRand == 1)
        {
            secondRand = Random.Range(1,8);
            for(int i = 0;i < secondRand; i++)
            {
                intPos = new Vector3(0,0,distPlayer);
                distPlayer += 1;
                GameObject GrassIns = Instantiate(Grass) as GameObject;
                GrassIns.transform.position = intPos;
            }

            if(firstRand == 2)
            {
                secondRand = Random.Range(1,8);
                for(int i = 0;i < secondRand; i++)
                {
                    intPos = new Vector3(0,0,distPlayer);
                    distPlayer += 1;
                    GameObject RoadIns = Instantiate(Road) as GameObject;
                    RoadIns.transform.position = intPos;
                }

                if(firstRand == 3)
                {
                    secondRand = Random.Range(1,8);
                    for(int i = 0;i < secondRand; i++)
                    {
                        intPos = new Vector3(0,0,distPlayer);
                        distPlayer += 1;
                        GameObject WaterIns = Instantiate(Water) as GameObject;
                        WaterIns.transform.position = intPos;
                    }
                }
            }
        }
    }
}
}

I'd really appreciate it if someone could tell me the mistake. Thanks!

Upvotes: 0

Views: 121

Answers (2)

Brad
Brad

Reputation: 4553

Your statement if (firstRand == 2) will never be reached because it's contained within the if (firstRand ==1) statement.

You need to structure your if statements like this:

if (firstRand == 1)
{
   ...
}

if (firstRand == 2)
{
   ...
}

if (firstRand == 3)
{
   ...
}

If you want to improve your code the following should work:

firstRand = Random.Range(1,4);
secondRand = Random.Range(1,8);
GameObject instance = null;

for (int i = 0; i < secondRand; i++)
{
    intPos = new Vector3(0, 0, distPlayer);
    distPlayer += 1;

    switch (firstRand)
    {
        case 1:
            instance = Instantiate(Grass) as GameObject;
            break;
        case 2:
            instance = Instantiate(Road) as GameObject;
            break;
        case 3:
            instance = Instantiate(Water) as GameObject;
            break;
    }

    instance.transform.position = intPos;
}

Upvotes: 1

Udontknow
Udontknow

Reputation: 1580

You have placed your code for the other objects inside the code group that is called when firstRand is 1. ( FirstRand ==2) and (FirstRand ==3) are at this position never true, you have written unreachable code.

Upvotes: 1

Related Questions