Need help with instantiating a prefab in c#

Okay I can usually instantiate things just fine. Here is my code, please tell me what I’m doing wrong!

public GameObject barricadePF; //assigned in the inspector
int random = Random.Range (0, 100);

		if (random >= 25)
		{
			Debug.Log ("test");
			Vector3 barriPos1 = new Vector3 ((2*Random.Range(1, 4)), nextTrack.transform.position.y, (nextTrack.transform.position.z+2.25f));

			barricade1 = Instantiate (barricadePF) as GameObject;

			barricade1.transform.position = barriPos1;
			barricade1.transform.parent = nextTrack.transform;	
			barricade1.name = "barricade"+(Movement.trackNum);
		}

		if (random >= 75)
		{
			Debug.Log ("test2");
			Vector3 barriPos2 = new Vector3 ((2*Random.Range(1, 4)), nextTrack.transform.position.y, (nextTrack.transform.position.z+2.25f));

			barricade2 = Instantiate (barricadePF) as GameObject;

			barricade2.transform.position = barriPos2;
			barricade2.transform.parent = nextTrack.transform;	
			barricade2.name = "barricade"+(Movement.trackNum+1);
		}

This all happens when the player collides with an invisible gameobject (inside the OnCollisionExit function).

What this SHOULD do is instantiate the barricade if the random number is greater than or equal to 25, and instantiate it twice if it is greater than or equal to 75.

What it actually does is instantiate the barricade if the number is greater than or equal to 75. It does, however, output both Debug.Log messages. I’m so stuck here, both blocks are exactly the same except for the numbers.One of them works, the other doesn’t.

UPDATE: After commenting out the whole second if block didn’t work, my problem seems to be that the first instantiation isn’t happening. Ever.

Hi, so you have to run only one of these conditions.
For second condition use

else if (random >= 75){

}

It will be launched only if first one was unsuccessful.

This code is all untested.

I would use a barricade handler with a list of barricade objects for easier control. (I don’t know much about the game you’re making so this may be entirely unecessary for your game type)

Example use:

public class SomeMainObject : MonoBehaviour
{

    BarricadeHandler barricadeHandler;

    void Start()
    {

        barricadeHandler = new BarricadeHandler();
        barricadeHandler.barricadePF = //Load prefab here
        barricadeHandler.nextTrack = //Pass nextTrack reference here

    }

    void OnCollisionEnter(Collider c)
    {

        //When your collision happens as before
        int random = Random.Range(0, 100);
        if(random >= 75)//Spawn 2 barricades 25% of the time
        {

            barricadeHandler.SpawnBarricade();
            barricadeHandler.SpawnBarricade();

        }
        else if(random < 75)//Spawn 1 barricade 75% of the time
        {

            barricadeHandler.SpawnBarricade();

        }

    }

}

barricade handler script:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;//For list

public class BarricadeHandler
{
    
    public GameObject barricadePF;

    List<GameObject> barricades = new List<GameObject>();

    public GameObject nextTrack;

    public void SpawnBarricade()
    {

        Debug.Log ("Spawning barricade");
        Vector3 barriPos = new Vector3 ((2*Random.Range(1, 4)), nextTrack.transform.position.y, (nextTrack.transform.position.z+2.25f));
 
         GameObject barricade = GameObject.Instantiate (barricadePF) as GameObject;
 
         barricade.transform.position = barriPos;
         barricade.transform.parent = nextTrack.transform;   
         barricade.name = "barricade"+(Movement.trackNum+1);
         barricades.Add(barricade);
         Debug.Log(string.Format("Barricade '{0}' spawned.", barricade.name));

    }

}