- Home /
Game Object is Instantiating on same position
I am working on a small project . But now I have come across a problem . The problem is the prefab is instantiating on the same position .
I want to instantiate a box end to end . The basic set up is as follows : -
Image 3 -- http://s10.postimg.org/a2cqtappl/image.png
Box game object has a parent empty object (Start_point) , and a child empty object (end point) . Those marks the Start and End of that game object .
The game a empty game object (Tile_Gen) which has a C# script as follows :-
using UnityEngine;
using System.Collections;
public class Tile_Gen : MonoBehaviour
{
public GameObject G_O_01 ;
public GameObject Current_Tile ;
public Vector3 Start_Position_Of_Path ;
// Use this for initialization
void Start ()
{
Start_Position_Of_Path = Current_Tile.transform.GetChild(0).transform.GetChild(0).position;
Instantiate(Current_Tile , Start_Position_Of_Path , Quaternion.identity);
}
// Update is called once per frame
void Update ()
{
Path_Generator ();
}
public void Path_Generator()
{
Instantiate(G_O_01 , Start_Position_Of_Path , Quaternion.identity);
// End of tile position = start tile position
Start_Position_Of_Path = G_O_01.transform.GetChild(0).transform.GetChild(0).position;
print (Start_Position_Of_Path);
}
}
Here G_O_01 is Start_point prefab , Current_Tile is Start_point .
But this is not instantiating as I wanted . This instantiates all the object at one place .
But I want something like one behind the another , like this :-
Image 4 -- http://s9.postimg.org/ctwm25m5b/image.png
So Please help me to figure it out .
Regards NB :)
Answer by Stefan02 · Jan 12, 2016 at 03:27 PM
Hello you only need to move Start Point for x meters ( x=lenght of path) every time when you instantiate new obj, its easy
Thanks for the reply :) . Can you elaborate ? Regards :)
Answer by JimNero_009 · Jan 13, 2016 at 12:53 PM
To elaborate on Stefan's answer; This seems to be a very complicated way of doing things. If you know the size of your prefab, why not just keep track of the position vector of each prefab you place? For example, at the start you instantiate one prefab at one given point which you can set publicly. Keep track of that vector in your code and after placing the prefab, increase the vector by one unit in the direction you want to go in and then place another prefab at that point. Continue until you get bored.