Transform.Forward dosen't work consistently

public void generateTrack()
print (“generate Track Called”);

    		//Setting the location on next track in line
    		nextTrackLocation = currentTrackLocation.forward * 10;
    		print ("nextTrackLocation before inst" + nextTrackLocation);
    		print ("currentTrackLocation before inst" + currentTrackLocation.position);
    		nextTrackLocation.x = this.gameObject.transform.position.x;
    		nextTrackLocation.y = this.gameObject.transform.position.y - 1f;
    		nextTrackLocation.z = this.gameObject.transform.position.z + 5f;
    		//print (nextTrackLocation);
    		//Instancing the next track in line
    		GameObject trackInstance = Instantiate(planeStraightPreFab, nextTrackLocation, Quaternion.identity) as GameObject;		
    		currentTrackLocation = trackInstance.transform;
    		print ("nextTrackLocation after inst " + nextTrackLocation);
    		print ("currentTrackLocation after inst" + currentTrackLocation.position);

I’m currently attempting to generate a track 10 units infront of the current track. The first track is predefined and works well but as soon as this function is called again the next track in line will spawn at the previous track’s position. The nextTrackLocation = currentTrackLocation.forward * 10; line does not seem to update the location.

Any help would be great. Thanks!

You’re misunderstanding what Transform.forward is. It gives you a direction relative to the transform object. So let’s say you have a transform at coordinates (10,10,10), facing along the z-axis. Transform.forward would give you the vector: (0,0,1), which is the direction the object is facing, relative to its position. The problem you are experiencing is that (I assume) all the objects are facing in the same direction, so all your calls to currentTrackLocation.forward give the same vector, which you are then multiplying by 10 and using as the position. Hopefully you can see why they are all being instantiated in the same place. There is a solution, however, and a simple one: I mentioned that the forward vector was relative to the position, so the fix is simple: add the position back on! If you change your code to set the location to this:

 nextTrackLocation = currentTrackLocation.position + currentTrackLocation.forward * 10;

You should get exactly what you need, and hopefully you can see why.