- Home /
Spawn Random Item On Trigger Enter
I have 3 different items and a box collider. When I walk into the collider I want to be given one of the 3 items at random. When I enter the trigger I want the item I get to spawn in front of me. I'm guessing that I need to make a empty gameObject as a spawnpoint. So I need to figure out how to get a random item from the collider and have it appear at the spawnpoint. Can anyone give me any help? That would be great! Steve
Answer by Dblfstr · Mar 03, 2014 at 07:36 PM
You will need an array or list of your 3 items (as prefabs).
public GameObject[] itemPrefabs;
Drag your 3 items into the GameObject array in the inspector.
Then you need your trigger/spawn script
OnTriggerEnter(Collider other){
//Gonna spawn stuff
}
Might need to check if the collider is the player
if(other.tag == "Player"){
//Ok, now we spawn suff
}
You will need a spawn function. You could do this inside the trigger function but here is a standalone function..
void Spawn(){
Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, Quaternion.identity);
}
Then add a bool to see if the spawner has spawned.
bool hasSpawned = false;
Attache this to an empty gameobject or other gameObject with a trigger collider. The item will spawn at the location of the trigger when the player walks into it. Total script:
Using UnityEngine;
Using System.Collections; //for random numbers
public class itemSpawner : MonoBehaviour
{
bool hasSpawned = false;
public GameObject[] itemPrefabs;
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
if(!hasSpawned)
{
hasSpawned = true;
Spawn();
}
}
}
void Spawn()
{
Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, Quaternion.identity);
}
}
Thanks for the script. When I add it in, it comes up with a few errors:
(18,60)The member 'System.Array.Length' cannot be used as method or delegate
(18,40)The best overloaded method match for 'UnityEngine.Random.Range(float, float)' has some invalid arguments
(18,40)Argument '#2' cannot convert 'object' expression to type 'float'
I also had to change the first line to:
public class itemSpawner : $$anonymous$$onoBehaviour
Otherwise it was a parcing error
I did type this on the fly, so I can accept the errors. You can use GetLength, ins$$anonymous$$d of length. And you could add an 'f' after 0, in random.range, to signify it is a float. I will make the edits to the script above. However, the logic is sound, and should work as you asked.
Don't add the f for float - it's an array, you do not want a float out of it.
Change it to:
itemPrefabs.Length
This is the correct line for Instantiate:
Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, Quaternion.identity);
fyi - in answer to 'random.range' expects two floats. Random.Range comes in 2 flavours:
The 'int' version: Random.Range(int $$anonymous$$, int max). Returns a value from $$anonymous$$ to max-1. Great for randomly choosing an index in an array.
The 'float' version: Random.Range(float $$anonymous$$, float max). Returns a floating point value from $$anonymous$$ to max. Good for randomly choosing positions and all sorts of fanciful things :)
Answer by kingames123456789 · Jul 19, 2017 at 01:07 PM
In this line :
void Spawn(){ Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position,
Quaternion.identity); }
How shuld I change it if I want to transform the position to another game object that is not in the array
it can be in the array , just to make it teleport to that object
Something like this :
void Spawn(){ Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, spawner.transform.position); }
Your answer
Follow this Question
Related Questions
Collision, but then only affect ONE of the gameObjects? 2 Answers
Trigger Spawning? 1 Answer
Random spawn of object on trigger 3 Answers
[solved] respawn when destroyed 1 Answer