- Home /
Problem with prefab parent and children objects that need to be instantiated multiple times.
I want to instantiate a parent with 4 children and after a trigger the parent to get destroyed leaving the children behind. Then a new instance of the parent with 4 children is created and so on. public GameObject Core; public GameObject Extend;
The children are are formed from 2 prefabs that i set in the prefab for the parent.
void Start()
{
GameObject core = Instantiate(Core, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
core.name = "Core(Clone)";
core.transform.parent = GameObject.Find("T-piece(Clone)").transform;
GameObject Ext1 = Instantiate(Extend, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
Ext1.name = "t 1";
Ext1.transform.parent = GameObject.Find("T-piece(Clone)").transform;
GameObject Ext2 = Instantiate(Extend, new Vector3(transform.position.x + 1, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
Ext2.name = "t 2";
Ext2.transform.parent = GameObject.Find("T-piece(Clone)").transform;
GameObject Ext3 = Instantiate(Extend, new Vector3(transform.position.x, transform.position.y + 1, transform.position.z), Quaternion.identity) as GameObject;
Ext3.name = "t 3";
Ext3.transform.parent = GameObject.Find("T-piece(Clone)").transform;
Debug.Log("Setup Complete");
Debug.Log(core.name);
}
This is how i instantiate everyone of the children. And then in the update after checking if one of the variables in Core(Clones)'s script is 2 I destroy the parent leaving the children.
void Update()
{
if (transform.Find("Core(Clone)").GetComponent<ObjectStateManager>().State == 2)
{
Debug.Log("Delete");
Transform Child = transform.Find("Core(Clone)");
Transform Child1 = transform.Find("t 1");
Transform Child2 = transform.Find("t 2");
Transform Child3 = transform.Find("t 3");
Child.parent = null;
Child1.parent = null;
Child2.parent = null;
Child3.parent = null;
Object.Destroy(this.gameObject);
}
}
The first iteration of this works perfectly fine. The objects get spawned and when State==2 the children get freed from the parent and the parent is destroyed. But the second time around nothing is instantiated in the world and i get the error NullReferenceException: Object reference not set to an instance of an object CreateT.Update () (at Assets/Scripts/CreateT.cs:33)
I've tried many things but nothing works. And the only thing i came across is that if i don't instantiate the children as i do now but just by doing Instantiate(Extend, new Vector3(transform.position.x, transform.position.y + 1, transform.position.z), Quaternion.identity)
they get instantiated in the world but that way i cannot access their script variables. Sorry if i didn't explain it clear enough i would be happy to reiterate. Thank you for your time.
Where is the error co$$anonymous$$g from? Is CreateT a spawner, and it tries to spawn the parent prefab on line 33, causing the error? Also, do both of the scripts you posted belong to the parent prefab?
So yes all the code parts belong to one script attached to an empty object(the parent) that has the job to instantiate his 4 children. This parent object is also a prefab that gets instantiated from another script entirely. Line 33 correlates to line 3 in the Update() code. I actually suspect that the problem lies within the lines around 3-14 in the Start (something to do with the way i'm instantiating).
Is the parent that this script is attached to the "T-piece(Clone)" you're setting the children's parent to be?
Answer by The_Three_Vs · Aug 21, 2020 at 06:24 PM
You use GameObject.Find to assign the created children's parent in the Start method, and then use transform.Find to get the child Core in the Update method. GameObject.Find searches the scene hierarchy for the object with the provided name, but transform.Find only searches the children of the object. Therefore, if there is more than 1 parent T-piece(Clone) in the scene, it is extremely likely that the children of the 2nd, 3rd, etc T-piece(Clone)'s are having their parents set to the 1st T-piece(Clone) in the scene. The non-1st T-piece(Clone)'s then try to find the Core(Clone) among their children, but it won't be there, so transform.Find will return null, and an error will be caused when trying to get the component of a null object.
Besides, there is a far simpler way to set the children's parent than core.transform.parent = GameObject.Find("T-piece(Clone)").transform;
. You could just use core.transform.parent = transform;
, or even assign the parent in Instantiate with GameObject core = Instantiate(Core, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity, transform) as GameObject
In fact, I don't think you even need as GameObject
, as Instantiate(GameObject ...
returns a GameObject.
One last thing - Using GetComponent every frame is resource-consuming. It would be better to store a reference to the core's script and then look at that reference every frame. E.g.:
public class CreateT : MonoBehaviour
{
public GameObject Core;
public GameObject Extend;
private ObjectStateManager objectStateManager;
void Start()
{
GameObject core = Instantiate(Core, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
core.name = "Core(Clone)";
objectStateManager = core.GetComponent<ObjectStateManager>();
...
}
void Update()
{
if (objectStateManager.State == 2)
{
...
}
}
...
}
Hope this helps!
Well thank you very much for the help this indeed helped and now for what it is works just as intended. I really hadn't done anything with parenting so all of the finds and parent methods i just found while searching through the forums. I didn't even know i could store a reference of a script. Anyways big help.
Your answer
Follow this Question
Related Questions
Photon Instantiate Prefab Script Problems 0 Answers
Don't allow prefab to spawn if prefab already exists at the spawn location. 1 Answer
Getting transform from an instantiated clone to parent it? 1 Answer
How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 Answers