- Home /
Instantiating a Prefab at Local position
Before anyone tells me to use the search bar, know that I have. Nothing I've found has helped. I'm just about ready to tear out my hair over this one...
Here is my code:
using UnityEngine;
using System.Collections;
public class InstantiatePrefab : MonoBehaviour {
public Transform thePrefab;
GameObject theObject;
// Use this for initialization
void Start () {
theObject = (GameObject)Instantiate(thePrefab, transform.position,transform.rotation);
theObject.transform.parent = transform;
theObject.transform.localPosition = transform.localPosition;
//Destroy (gameObject);
}
}
From what I've read, this SHOULD instantiate the object and ensure that it goes to the proper position. Indeed, it does go to my the instantiating object's coordinates, but in WORLD space. I should mention that the object calling this script is a child of a Prefab. Any help would be greatly appreciated!
Answer by EelcoJannink · Mar 16, 2017 at 11:22 AM
A bit old, but :
GameObject obstacle = Instantiate(prefabList[1], this.transform, false ) as GameObject;
the false for instantiateInWorldSpace avoids the prefabs position being concidered worldcoords.
I literally can not thank you enough for this one simple post. I don't care how old it was,; this was all I wanted to know. Every single thread I would go to was the same "You should have read the scripting reference ". THAN$$anonymous$$ YOU!!! Also it wasn't just this one simple transform issue that was taking me so long; I was working on other things but still, thank you so much dude.
I cannot thank you enough for the comment. That solves my problem.
Answer by ilusja · Dec 05, 2018 at 04:41 PM
Hey, it works for me. Script attached to object will be a parent.
var go = Instantiate (prefab, transform.position + new Vector3(x, y, z), Quaternion.identity);
go.transform.parent = transform;
The important part is: you have to add transform.position + before the new position so your code can actually use parent's transform.position before placing new object. Notice, that the parent actually becomes the parent after instantiating so that's why it uses world position to instantiate the object.
Answer by unityDevloper · Mar 16, 2017 at 08:32 PM
May be you can do like this...
theObject = Instantiate (currentPillar) as GameObject;
theObject .transform.parent = parentObject.transform;
theObject.transform.localPosition = yourPosition;
Answer by senad · Jan 15, 2013 at 09:45 AM
Maybe I am not understanding you right. :)
But the child's local position is relative to the parent's world position. So if you want the child to be at the same spot as the parent, you just have to set its local position to the zero vector.
If you change that vector, the child will move, but relative to the parent. You can just experiment with the values in the editor to understand what they mean. :)
I've tried that as well. I think Unity might be getting confused since the object running the script is already a child of another object. Said object is very close to the zero vector within its parent, so Unity instantiates the object close to the world's zero vector. However, I thought that changing the local position after this happened would fix that problem.
Hmm... Well, this is strange. It would seem that the object IS instantiating where I want it, but its transformation gizmo is staying at the world zero vector...
Your answer
Follow this Question
Related Questions
Instatiation on the wrong position 1 Answer
Camera rotation around player while following. 6 Answers
How to instantly move a newly instantiated prefab? 2 Answers
instantiated object not in right position 1 Answer
Unity 5.6 doesn't instatiate object mantaining child position driven by animations 0 Answers