- Home /
Attach object when NOT in Hierarchy
Hey, I'm new to Unity, which makes me pretty bad at it. I'll try to explain my problem as good as I can.
I have an object saved, which I will spawn when a certain thing happens. I want to attach an object to that object, but it only works when that object is out in the Hierarchy. How do i solve this?
Pics:
When the object is saved, but not out in the Hierarchy. (doesnt work to attach)
When the object is out in the Hierarchy, it's possible to attach them.
How do I fix this? Is it a way?
You are best off finding the Player1 and Player2 objects via your script on the power up at run time. If either of your player objects ever gets destroyed then make sure you have a null reference check in this script that will then try to find these objects again should they not exist anymore(which will also stop any code that uses these objects from running until they are found).
These links may prove useful to you :
http://answers.unity3d.com/questions/47830/what-is-a-null-reference-exception-in-unity.html
http://docs.unity3d.com/ScriptReference/GameObject.Find.html
Answer by Kiwasi · Nov 24, 2014 at 05:53 PM
This cannot be done.
This is because you can instantiate a prefab in any scene. Unity has no guarantee that the hierarchy object will exist at runtime. You can link in another prefab. Or you can use GameObject.Find via script to get an instance at runtime.
Your answer
Follow this Question
Related Questions
Does placing things in the hierarchy create objects? How do I reference those objects in code? 1 Answer
How to change object's hierarchy level in animation? 6 Answers
How Do I Programatically Attach Objects To Each Other? 3 Answers
Question about animation 2 Answers
How can I attach objects like a picture? 2 Answers