- Home /
How to add in my script that my prefabs instantiate as children of the player character who is also an instantiated prefab?
Hi there, this is my first post here so apologies if I mess something up. Below is the code I currently have for my spawner. It works, but I need the clones to become children of the player character when they spawn into the game world. The problem is, the player is also an instantiated prefab, and I'm not quite sure how to add this functionality to my script. Any help would be appreciated.
using UnityEngine;
using System.Collections;
public class ChaserSpawner : MonoBehaviour
{
//The method of spawning (random = choose a spawn point randomly; onebyone = spawn an object on spawnpoint #1 , the next will be spawned on spawnpoint #2 ,...)
public enum SpawnType { Random, OneByOne }
[Header("Main parameters")]
public SpawnType spawnType;
public GameObject[] spawnPoints; //The array that will contain the different spawn points
public string toSpawnResourceName = "Chaser1";
[Header("Extra parameters")]
public int numberOfObjectsToSpawnOnContact = 1;
public int maxGameobjectsToSpawn = 0;
private int nextSpawnPointIndex = 0;
private int spawnedObjects = 0;
private GameObject toSpawn;
// Use this for initialization
void Start()
{
toSpawn = Resources.Load("Chaser1") as GameObject; //Load the gameobject to spawn from resources
}
void OnTriggerEnter2D(Collider2D other)
{
if (GetComponent<Collider2D>().tag == "Player") //Check for what you want about the collider here
{
SpawnAnObject();
}
}
private void SpawnAnObject()
{
if (spawnedObjects >= maxGameobjectsToSpawn) //If enough objects spawned , stop here
return;
for (int i = 0; i < numberOfObjectsToSpawnOnContact; i++)
{
GameObject spawnPoint = spawnPoints[0]; //Set a default spawnpoint to avoid errors
if (spawnType == SpawnType.Random)
{
spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
}
else if (spawnType == SpawnType.OneByOne)
{
spawnPoint = spawnPoints[nextSpawnPointIndex];
nextSpawnPointIndex++;
if (nextSpawnPointIndex >= spawnPoints.Length)
nextSpawnPointIndex = 0;
}
Instantiate(toSpawn, spawnPoint.transform.position, Quaternion.identity); //Spawn the wanted GameObject
spawnedObjects++; //Increment the number of spawned object
}
}
}
Answer by unidad2pete · Aug 20, 2017 at 10:10 PM
You only need a reference to your player, and make transform of this reference parent of your spawned object, this is your code, I only added 3 new lines, with this simbol on top line / -------------------------------- /
Should be works fine
/* -------------------------------- */
public Transform player; // Or transform you need set parent of your spawned objects
//The method of spawning (random = choose a spawn point randomly; onebyone = spawn an object on spawnpoint #1 , the next will be spawned on spawnpoint #2 ,...)
public enum SpawnType { Random, OneByOne }
[Header("Main parameters")]
public SpawnType spawnType;
public GameObject[] spawnPoints; //The array that will contain the different spawn points
public string toSpawnResourceName = "Chaser1";
[Header("Extra parameters")]
public int numberOfObjectsToSpawnOnContact = 1;
public int maxGameobjectsToSpawn = 0;
private int nextSpawnPointIndex = 0;
private int spawnedObjects = 0;
private GameObject toSpawn;
// Use this for initialization
void Start()
{
toSpawn = Resources.Load("Chaser1") as GameObject; //Load the gameobject to spawn from resources
/* -------------------------------- */
player = GameObject.FindGameObjectWithTag("Player").transform; //Find the player gameObject, make sure have tag named Player
}
void OnTriggerEnter2D(Collider2D other)
{
if (GetComponent<Collider2D>().tag == "Player") //Check for what you want about the collider here
{
SpawnAnObject();
}
}
private void SpawnAnObject()
{
if (spawnedObjects >= maxGameobjectsToSpawn) //If enough objects spawned , stop here
return;
for (int i = 0; i < numberOfObjectsToSpawnOnContact; i++)
{
GameObject spawnPoint = spawnPoints[0]; //Set a default spawnpoint to avoid errors
if (spawnType == SpawnType.Random)
{
spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
}
else if (spawnType == SpawnType.OneByOne)
{
spawnPoint = spawnPoints[nextSpawnPointIndex];
nextSpawnPointIndex++;
if (nextSpawnPointIndex >= spawnPoints.Length)
nextSpawnPointIndex = 0;
}
GameObject ObjectSpawned = Instantiate(toSpawn, spawnPoint.transform.position, Quaternion.identity); //Spawn the wanted GameObject
/* -------------------------------- */
ObjectSpawned.transform.parent = player; // Set the spawned Object, child of player
spawnedObjects++; //Increment the number of spawned object
}
}
Hey unidad2pete, thanks a lot for the quick and helpful response! I'm not getting any errors from the modified script you gave me, but now nothing is spawning. Any idea what might be causing that to happen?
Are you sure? the objects are inside of player in Hierarchy. if your prefab to be spawned have any script add a console print on start function to make sure is not spawned please.
void Start()
{
print(" I spawned !! my position = " + transform.position);
}
and one more print to check if the player transform is not null:
print(player.gameObject.name);
after the line when instantiate the spawn for example
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can i check/wait until the gameobject will end the rotation ? 1 Answer
How can I call the Load method and/or the ShootingSettings method also only once in the Update ? 1 Answer
Integer value randomly multiples itself by 3 or 2 for seemingly no reason. 2 Answers