- Home /
How do I make prefab follow a gameobject?(C#)
Hi. I have been trying to get a prefab instantiate and then follow a gameobject in C#. The prefab is instantiated in the script that is attached to the gameobject, but the prefab doesn't change its position once it has been instantiated.
void Start ()
{
Vector3 firePosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
Instantiate(FirePrefab, firePosition, Quaternion.identity);
}
I have tried to put the firePosition in the update function, but I am not sure how to get that to work (Is that even the right way to do it?)
Does anyone know how I can do this? Thanks in advance :)
Yeah, just parent it. You'll need a local variable to store your prefab though so you can access its transform. You don't need to construct a new vector3 like that either, you can just use tranform.position :)
Convert one of the comments to an answer so we can resolve this question.
Answer by Rambit · Feb 23, 2013 at 09:53 PM
Wow, now I feel really stupid... That worked, thanks!
EDIT: Making the prefab a parent of the gameobject solved it.
Your answer
Follow this Question
Related Questions
gameObject are not referenced 2 Answers
Trouble with destroying an instantiated prefab 2 Answers
How to create GameObject without adding it to scene? 1 Answer
Thrown objects always have same rotation 2 Answers