- Home /
Parenting an instantiated Prefab... again
There seem to be a lot of question concerning this and while some of them seem to have working solutions, they do not work in my project no matter what I do.
An elevator platform should spawn a prefab model when function MakePillar is called. This model should then be parented to the platform. All code is included in the Elevator's script, so no third objects are involved.
var PillarPrefab : GameObject; // The Pillar model that appears below the elevator platform. The function gets called in the Update function when a key is pressed.
function MakePillar()
{
var Pillar : GameObject = Instantiate(PillarPrefab, transform.position, transform.rotation ) ;
Pillar.transform.parent = transform ;
}
What works: The first part, i.e. creating the pillar at the correct position. What doesn't work: the second part, i.e. parenting the new clone to it's parent, the elevator.
I already read and tested the code from the following threads...
http://answers.unity3d.com/questions/428578/parenting-an-instantiated-prefab.html
http://answers.unity3d.com/questions/55796/instantiate-a-prefab-to-a-parent.html
http://answers.unity3d.com/questions/428578/parenting-an-instantiated-prefab.html
The code:
Pillar.transform.parent = transform ;
is trying to set the parent of your pillar to the transform of nothing. You will need to reference the elevator in order to get the correct transform. If the script is attached to the elevator gameObject then you should be able to set it with.
Pillar.transform.parent = gameObject.transform ;
if the script is not attached to the elevator you will need to refrence the elevator gameObject with something like:
Pillar.transform.parent = GameObject.find("Elevator").transform;
The script is attached to the elevator so the simple "transform" is enough for it to refer to the object it is attched to. Either way, it makes no difference if it's transform or gameObject.transform :(
how about:
var Pillar : GameObject = GameObject.Instantiate(PillarPrefab, transform.position, transform.rotation ) ;
It doesn't seem to change anything. However, something else in my script makes problems because the function itself works in an otherwise empty script (see below).
Answer by 1337GameDev · Apr 15, 2013 at 04:58 PM
Use debug statements to check if transform is null and such and print to the console. After your code try this: Debug.Log("parent is: " + Pillar.transform.parent.name);
If that gives a null reference, then the parent is not being saved. If so, then the parent is being applied but is being removed later. Make sure to run your instantiation code once or unity might create a ton of objects. Create a Boolean and just flip it when you run the code so it only runs once.
Thanks for your comment.
I did everything you suggested and it gives back the parent name correctly in Debug.Log("parent is: " + Pillar.transform.parent.name);
I'm really not sure why this is not working and it's starting to become extremely annoying because now I have to find a workaround :(
If it prints it out, then something else must be changing it. Or the object (parent) is being destroyed. $$anonymous$$ake sure no other scripts are modifying this. Also make sure that you don't modify it anywhere else. If need be, create a test script and test the parenting in that script with all other scripts removed from all objects in a scene (or create a test scene that is empty).
Also if you have any null references (some could be silent) unity can act weird. Start with an empty scene with just a test script and 2 primitive objects from unity.
If that fails, then create a separate new project (with no 3rd party or custom assets imported) and try again. If that fails, then reinstall unity is your best bet.
Heh, that actually works, now I'll check which pieces in the original code are making trouble and failing that, I'll just put it in a seperate script and cross-reference.
thanks!
Edit: After testing some more, I found out that the original script wouldn't work in any asset I already had in my scene, but if I create a new one in the editor and attach the script to it and effectively replicating my old elevator, it works like a charm. It's BS and it needlessly cost me several days but at least now it works. Thanks for all suggestions :)
Days have passed and I finally figured out what caused the problem: Another script attached to the elevator that was supposed to parent objects on it and unparent them when they are no longer on the platform. Unfortunately, I sued the Detach.children command and thus, ALL children got detached :) Always try to check ALL scripts an object has, not just the one that seems to make problems :)
Your answer
Follow this Question
Related Questions
GameObject parenting during runtime 1 Answer
Instantiate New Gun 1 Answer
How to Parent a Cloned Object to Another Cloned Object 1 Answer
Instantiate Clones Itself - Rather than Prefab 0 Answers
Instantiate a Prefab as child 0 Answers