- Home /
onTrigger spawn object C#
Hi everybody
I am pretty new to programming, and just are trying to figure it all out. Maybe someone would like to give me some advise.
I am working on a game, where a spawnPoint(emptyGameObject) is flying through a group of quads(isTrigger). The spawnPoint should spawn one of three fruits (if there is none) which should fly with the spawnpoint.
short:
check if flying through trigger
check if there is no fruit (i could not figure it out yet)
if both yes, spawn fruit
choose random fruit from array
let the fruit fly with spawnPoint
I got the array, but there is no spawn when I play it:
public class Fruit_Spawn_Trigger : MonoBehaviour {
public GameObject[] myFruit;
void OnTriggerEnter(Collider other)
{
Debug.Log ("Trigger active");
int myFruit_num = Random.Range(0,2);
if (other.gameObject.tag == "Fruit_Spawn_Trigger")
{
Instantiate(myFruit[myFruit_num], transform.position, transform.rotation);
}
}
}
Debug Log:
"Trigger active" //so far so good
IndexOutOfRangeException: Array index is out of range. Fruit_Spawn_Trigger.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Fruit_Spawn_Trigger.cs:25) //cs:11 on my post. I deleted some (//)...whats wrong here?
Answer by Leuthil · Mar 31, 2014 at 12:30 PM
You should be able to do Random.Range(0, myFruit.length) which will always work no matter how many fruit you put in your myFruit array in the Editor. And actually @OSG Random.Range(int, int) is actually exclusive so using Random.Range(0, 2) will only return 0 or 1 and never 2. As it was discussed in this question.
As for your question about determining if there are no fruit... the best way to do this is to make a List of fruits and keep adding new fruits to the List when they are created and removing them when they are destroyed. Then you can simply do List.Count > 0 to see if there are any fruit. The reason you use a List instead of an Array is because a List can shrink/expand it's size dynamically whereas an Array has a set size when it is first initialized. Here is some code that could help you get started:
// you must include this to have the List class available
using System.Collections.Generic;
public class Fruit_Spawn_Trigger : MonoBehaviour {
public GameObject[] myFruit;
private List<GameObject> aliveFruits = new List<GameObject>();
void OnTriggerEnter(Collider other)
{
Debug.Log ("Trigger active");
int myFruit_num = Random.Range(0,myFruit.length);
if (aliveFruits.Count > 0 && other.gameObject.tag == "Fruit_Spawn_Trigger")
{
GameObject newFruit = Instantiate(myFruit[myFruit_num], transform.position, transform.rotation) as GameObject;
aliveFruits.Add(newFruit);
}
}
}
Answer by Dacardi · Apr 01, 2014 at 10:24 AM
Great, thank you for your answer!
It did not like "lenght", so I changed it to "Length". But there is still no spawn.
So I made the "List aliveFruits" public, to see what happens. The value rises with each passed quad but in the list no "Fruits" but "Missing(Game Object)" are added.
Answer by dwalker350 · Aug 23, 2015 at 01:29 AM
The "Missing(GameObject)" seems to indicate that the instantiate is returning a null pointer, which could happen if the fruit's GameObject is never set. Check the value of the fruit before spawning using Debug.Log(myFruit[myFruit_num);
or Debug.Assert(myFruit[myFruit_num] != null, "Fruit #" + myFruit_num + " has not been set!");
)
Your answer
Follow this Question
Related Questions
Instantiate 1 at a time at each transform in array 1 Answer
My Spawn Point doesn't ignore random game objects. How do I fix? 1 Answer
How Do I Add An Instantiated Object To An Array? 3 Answers
How do i prevent an object instantiating another object straight away 2 Answers
Random select from array and spawn 1 Answer