- Home /
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.
The reason both debug messages are shown is because when your random >= 75 condition is met the random >= 25 condition is also true because 75 is already bigger than 25 making them both fire
I want them to both fire, but the first instantiate doesn't ever happen (and I want it to)
Answer by xortrox · May 06, 2014 at 02:49 PM
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));
}
}
No problem, please accept the answer so others will know it has been answered. :)
Answer by YoungDeveloper · May 06, 2014 at 02:21 PM
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.
if that were true, it wouldn't output both Debug.Log statements. I tried it anyway, to no avail :(
then just change them in places ? :p That's a silly solution but will work.
if (random >= 75){
}
else if (random >= 25){
}
That is, by understanding what You want.
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.
nope, didn't work. Outputs "test2" and instantiates when random >= 25, only outputs "test" when random >= 75, so the instantiation still isn't happening, and using the else does not make the other condition happen as well
That's not how it's done anyway, you have a lot of unneeded code duplication there.
public GameObject barricadePF; //assigned in the inspector
private void Start(){
Spawn();
}
private void Spawn(){
int random = Random.Range (0, 100);
int spawnAmount = 0;
if (random >= 25) spawnAmount = 1;
if (random >= 75) spawnAmount = 2;
Debug.Log("Spawn "+spawnAmount+" barricades");
if(spawnAmount != 0){
for(int i = 0; i < spawnAmount; i++){
Vector3 spawnPos = new Vector3 ((2*Random.Range(1, 4)), nextTrack.transform.position.y, (nextTrack.transform.position.z+2.25f));
GameObject obj = Instantiate (barricadePF, spawnPos, Quaternion.identity) as GameObject;
}
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Instantiating on Input 1 Answer
Instaniate Prefab 1 Answer
Multiple Cars not working 1 Answer
Instantiate ground/enemies 1 Answer