- Home /
Spawn item at different location
I have a box collider that is going to be used as a trigger and when my player enters the trigger, it spawns 1 out of 3 random items that I made. Here is the script I've been using, thanks to Dblfstr who helped create it.
Using UnityEngine;
Using System.Collections; //for random numbers
public class itemSpawner : MonoBehaviour
5.{
bool hasSpawned = false;
public GameObject[] itemPrefabs;
10. void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
if(!hasSpawned)
15. {
hasSpawned = true;
Spawn();
}
20. }
}
void Spawn()
{
25.
Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, Quaternion.identity);
}
}
Now what I want to change is where the item spawns. It currently spawns at the location of my trigger. I want to create an empty gameObject and use that as my spawn location. Then I want to parent the spawn location to my player so that I walk through the trigger and then the item will spawn at the empty gameObject and will follow my player, since the spawn point will be parented to my player. Can I make a spot in my code that will allow me to drag an empty gameObject in the inspector onto it? I'm not sure how to add this to my code so any help would be great, thanks! Steve
Answer by wiiarethesound · Mar 06, 2014 at 08:17 PM
Declare a public variable first:
public Transform spawnLocation;
And then drag a Game Object from your scene into the Inspector. Then in Spawn()
void Spawn()
{
Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)], spawnLocation.position, Quaternion.identity);
}
I'm not sure why you want to parent the spawn transform to your player though. Can you explain why please?
I tried putting that in the code but it's co$$anonymous$$g up with a couple errors. The reason I want the item to spawn at the player and then follow the player is because I want the item to spawn in front of the player. And eventually I want to have the item stay in front of the player until the player decides to shoot it. So the item spawns and stays at the spawn location which is parented to the player, then the player presses the space bar and it shoots forward. Haven't got that far yet, but that's the plan.
Fixed it actually, it should be spawnLocation not spawnTransform at the bottom
I think you mean
Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)], spawnLocation.position, Quaternion.identity);
I, too, am curious why you want to new object to follow you around. The code above to spawn at a different location will not make the item follow you, however, it will just allow you to choose a spawn location, the item will not be a child of that object by default.
So your Transform spawnLocation would need to be a child of your Player. Then after you Spawn your item, you sill need to make it a child of that gameObject.
Something like this:
void Spawn()
{
GameObject item = (GameObject)Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],transform.position, Quaternion.identity);
item.transform.parent = spawnLocation;
}
Thanks, that worked now. Except, its spawning directly on top of my player. Can I make it sit just in front of the player?
That depends on the position of the spawnLocation gameObject, and the position of your prefabs. When you drag a prefab into the scene, where is it loacted? Is it in the air, on the ground, etc. You will have to play with the values. I would set my prefab location at 0,0,0. Then it should (theoretically)spawn at the origin of the spawnLocation gameobject.
Answer by Dblfstr · Mar 06, 2014 at 08:47 PM
Using UnityEngine;
Using System.Collections;
public class itemSpawner : MonoBehaviour
{
// Variables //
/* hasSpawned to check if the items trigger has */
/* spawned an item yet, false by default */
/* Array of your item prefabs to be spawned */
/* these are set in the inspector */
/* Gameobject item, this will hold your */
/* spawned item. */
/* Transform spawnLocation, this is where */
/* your items will spawn. ( set in inspector) */
/* (spawnLocation is a child of player) */
bool hasSpawned = false;
public GameObject[] itemPrefabs;
public GameObject item;
public Transform spawnLocation
//When the player enters the trigger
// Check that it is the player, and that
// the trigger has not yet spawned an
// item. If these criteria are met, call
// the spawn function, and set
// hasSpawned to true.
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player" && !hasSpawned)
{
hasSpawned = true;
Spawn();
}
}
//Spawn function will randomly spawn one of your
// item prefabs at the spawnLocation's current position
// and rotation. This will only be called the first
// time the trigger is entered.
void Spawn()
{
item = (GameObject)Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],
spawnLocation.position,
Quaternion.identity);
//Make the item a child of the spawnLocation GameObect.
item.transform.parent = spawnLocation;
}
}
Should I update my whole code to this? What are the major changes?
Yes.
I created the two new variables: spawnLocation, and item.
public GameObject item;
public Transform spawnLocation
In the spawn function, I set item to the Instantiated prefab, so we could access its transform. Then I set it as a child of the spawnLocation.
void Spawn()
{
item = (GameObject)Instantiate(itemPrefabs[Random.Range(0,itemPrefabs.Length)],spawnLocation.position, Quaternion.identity);
item.transform.parent = spawnLocation;
}
I added a bunch of comments. And I cleaned up the trigger functtion to combine the two if statements into one statement:
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player" && !hasSpawned)
{
hasSpawned = true;
Spawn();
}
}
Also, go ahead give @wiiarethesound a thumbs up for the spawnLocation.
The objects are still spawning on my player. How do I change the values of the prefabs so they spawn in front of the player?
Place the empty gameObject spawnLocation where you want them to spawn (in front of your player). Then parent the gameObejct to your player. The item will spawn where that gameObject is.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135544im_/https://answers.unity.com/themes/thub/images/avi.jpg)