- Home /
Problem is not reproducible or outdated
Instantiate prefab in correct path
Whenever I instantiate a new prefab, its path is a bit off.
As you can see from this image, the clone (i.e. loaded prefab) is being loaded into the bottom of the "content" parent.
I'm trying to load the prefab so that it loads beneath "SingleOpretContainer"
.
My code for doing this is :
vContent = GameObject.FindGameObjectWithTag ("scrollSingle");
Instantiate (prefab, vContent.transform);
"scrollSingle" is the tag for Content" in the hiearchy.
How do I fix its position?
Answer by look001 · Sep 02, 2017 at 09:49 PM
To move the GameObject up in the hierearchy you have to do this:
public void MoveUpInHierarchy(int amount) {
int i = transform.GetSiblingIndex();
transform.SetSiblingIndex (i - amount);
}
you can use this approach to move your gameobject to the top with 0 as the parameter of SetSiblingIndex
Interesting approach. Thank you for the advice.
However, my prefab is still loaded at the bottom of my parent. Do you have any suggestions, on how I fix it as mentioned in my original post?
Answer by MaxGuernseyIII · Sep 02, 2017 at 11:43 PM
I don't know if using a tag to find the parent object & transform is the way to go there and I suspect that's what is causing this. Try adding a public field of type GameObject (I usually call it "target" or "container"). In the inspector, directly drag the object into which you want to populate prefabs onto the editor for that field in your script. That should do the trick. If not, you can always use transform.SetParent to force the parent to be the transform you want.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Terminal-like GUI, wait for input 1 Answer
Spawned tree not falling 1 Answer
Problem with blending two terrains together on an edge. 1 Answer