# 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 :- Please see the comment below

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 :-

Regards NB :)

You'r approach is rather strange, but stick with it an learn it's faults / strengths because creating it your way is a great way to learn. Below is the same concept just cleaned up a bit. Remember that in your setup, the box object has to be offset so the left side of the box is at the same position as its parent. This can be done with sprites setting the pivot to the right side, or by offsetting the childs transform.

using UnityEngine;

public class Spawner : MonoBehaviour
{

public GameObject Prefab;

private GameObject _previousTile;
private Vector3 _nextStartPosition;

void Update()
{
PathGenerator();
}

private void PathGenerator()
{
_previousTile = Instantiate(Prefab, GetNextStartPosition(), Quaternion.identity) as GameObject;
}

private Vector3 GetNextStartPosition()
{
if(_previousTile == null)
return Vector3.zero;

var BoxObject = _previousTile.transform.GetChild(0);
if(BoxObject == null)
return Vector3.zero;

var endOfLastTile = BoxObject.GetChild(0);
if(endOfLastTile == null)
return  Vector3.zero;

return endOfLastTile.position;
}
}

• Thanks for the answer but little late. I have already figured it out :) – Nicolus Buck Feb 14 '16 at 3:35

Here you are giving same point every time. Second bad practice is you are calling your generator method in Update.

Well, if you want to spawn platforms in linear path then you adopt ambiguous way. You made an empty parent, then child (platform) then then again child gameObject for End Point. It would bevery confusing because of global and localPositions.

Here is a simpler way to achieve your goal.

• Delete parent and child and make prefab of only box.

using UnityEngine;
using System.Collections;

public class Tile_Gen : MonoBehaviour
{

public GameObject G_O_01 ;

GameObject _lastTile;
float _tileGap = 0;

// Use this for initialization
void Start ()
{
_lastTile = Instantiate(G_O_01) as GameObject;
_tileGap = _lastTile.transform.localScale.x/2.0f;

// Creating 20 tiles
for (int i = 0; i < 20; i++)
Path_Generator ();
}

// Update is called once per frame
void Update ()
{

}

// A general method, whenever you call it, it will spawn tile next to previous one.

public void Path_Generator()
{
_lastTile = Instantiate(G_O_01 , new Vector3(_lastTile.transform.position.x + _tileGap ,_lastTile.transform.position.y,_lastTile.transform.position.z) , Quaternion.identity) as GameObject;
}

}


Now set you block in editor to very start (at very left) then make prefab or apply changes. After that drop your GameObject to this script. Because this script is spawning tiles from left to right.

Hope this helps.

• So can you tell me where I went wrong ? Why this 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); } is generating at same position . According to that I have accessed End position of last object and assign it to the new starting position . But why It is generating at same point ? Regards NB :-) – Nicolus Buck Jan 12 '16 at 6:01
• I don't know your logic, but I'd tell you what is obvious. Because you are accessing your G_O_01 every time to update your Start_Position_Of_Path. Keep in mind that Instantiate(G_O_01) will make no impact on G_O_01, as Instantiate will create and return a copy of that object. Here G_O_01 is like a blue print. – Hamza Hasan Jan 12 '16 at 6:30
• Ok , Thanks for the reply . Finally figured it out where I went wrong :) – Nicolus Buck Jan 12 '16 at 8:42
• Good to listen that :) – Hamza Hasan Jan 12 '16 at 8:43
• BTW you can use the suggested solution as it is more easy. I think :) – Hamza Hasan Jan 12 '16 at 8:45

This is because you Instantiate them in the same position:

Instantiate(G_O_01 , Start_Position_Of_Path , Quaternion.identity);
Start_Position_Of_Path = G_O_01.transform.GetChild(0).transform.GetChild(0).position;


You could try something like this in your Start ():

float n = 5.0f;
Vector3 dist = ((endPoint - startPoint)/n);
for( int i = 0; i <= n; i++ ) {
Instantiate(G_O_01 , startPoint + dist*i , Quaternion.identity);
}


This code will Instantiate n+1 G_O_01 objects on straight line between endPoint and startPoint ( including both points ).

Edit:

Why your code spawns them in one position?

In short. You place object in position Start_Position_Of_Path. Then you set Start_Position_Of_Path to this object's position ( which is of course equal to Start_Position_Of_Path ), and spawn next object in Start_Position_Of_Path, etc. etc.

• So can you tell me where I went wrong ? Why this 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); } is generating at same position . According to that I have accessed End position of last object and assign it to the new starting position . But why It is generating at same point . – Nicolus Buck Jan 12 '16 at 6:00
• Regards NB | :--) – Nicolus Buck Jan 12 '16 at 6:00
• @NicolusBuck updated answer. – Marqin Jan 12 '16 at 11:16