Transform position

For some reason, even though I set transform.position, my cars/trucks/logs keep instantiating in the center of the lane. The transform.position and transform.rotation inputs don’t seem to be absorbing the position and rotation of the car prefab. Could you tell me what I’m doing wrong?

public class ObstacleSpawner : MonoBehaviour {

    public GameObject Obstacle;

    public float SpawnInterval = 4;
    public float WaitTime = 0;
    public float DestroyTime = 12;


	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if (Time.time > WaitTime)
        {
            Spawn();
            WaitTime += SpawnInterval;
            SpawnInterval = Random.Range(1, 6);
        }
	}

    void Spawn ()
    {
        var newObstacle = Instantiate(Obstacle, transform.position, transform.rotation, transform);
        Destroy(newObstacle, DestroyTime);
    }

}

Hi,

When you say the “car prefab”, what exactly refers to that in your code?

From what I can tell, you are setting the position and rotation of newObstacle within the Spawn() method to that of the ObstacleSpawner.

If you could provide more information it would be useful.

I solved the problem!! The issue was that I had set the transform on the car object, not on the spawner. I’m not sure why that was a problem, but the script only works when the object’s initial transform is set from the spawner.

1 Like

Glad to hear you have resolved the problem Jackie. :slight_smile:

Privacy & Terms