The question is answered, right answer was accepted
Give a instantiated prefab a parent
I would like to make an instantiated object the child of the object it spawns on. The object it spawns on is an empty game object.
It keeps giving me an error "setting parent of a transform in a prefab is disabled to prevent data loss"
The ultimate goal is to have the object "container" spawn on the empty game object as its child. I am fairly sure i am doing something wrong with the transform, but i can't figure out what. :s
The containers are only spawned in once when the game starts.
Instantiate(container, new Vector3(currentX, currentY, currentZ), Quaternion.Euler(-90, 0, 0));
container.transform.parent = transform;
Answer by OctoMan · Sep 27, 2016 at 10:50 AM
public Transform yourParent;
container.transform.SetParent(yourParent); // needs to be of type transform
Alright, so i did that, now my code is:
public Transform containerStorer;
container.transform.SetParent(containerStorer); // needs to be of type transform
containerID = containerID + 1;
Which produces the error:
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
At first i indeed had it at a prefab which was already in-game, then i set it on a empty game object, which is in no way a prefab, later on i also put the script on that empty game object, but i keep getting this error.
Create any GameObject again.
Put the script piece anywhere on any other gameobject or prefab which instantiate the container.
In the inspector drag the new GameObject In the container slot. If that works, you had any mistake before.
If that doesn't work, restart Unity and $$anonymous$$onodevelop. You might encountered a bug.
Still not, restarted computer so unity etc with it. Code:
Instantiate(container, new Vector3(currentX, currentY, currentZ), Quaternion.Euler(-90, 0, 0));
container.transform.SetParent(containerStorer); // needs to be of type transform
Image 1 is of the script GameOobject, Image 2 is the emty object, they both arent prefabs.
Well, still doesn't work but technically your answer is correct, Will see if/how i can change instantiating it.