Instantiated Object spawning at wrong position

I’m trying to make an object/objects instantiate when OnTriggerEnter is activated.

At first this worked fine, the objects appeared when the trigger was entered just as needed.

Original script-

using UnityEngine;
using System.Collections;

public class HazzardSpawner : MonoBehaviour
{

	public GameObject hazzardType;

	void OnTriggerEnter()
	{
		Instantiate (hazzardType, transform.position, transform.rotation);
		Destroy(gameObject);

			}
}

Now I get a little more technical and want to add a variation along the x axis to where they spawn.

new script-
using UnityEngine;
using System.Collections;

public class HazzardSpawner : MonoBehaviour
{

	public GameObject hazzardType;
	public int spawnVariation;

	void OnTriggerEnter()
	{
		Vector3 spawnPosition = new Vector3 (Random.Range (-spawnVariation, spawnVariation), 0, 0);
		Instantiate (hazzardType, spawnPosition, transform.rotation);
	}
}

It adds the variation along the x axis, but for some reason its also affecting where the objects spawn along the z axis. Every time its spawning the objects about 20 units away from the origin point on the Z axis.

???

42384-spawn1.png

This is the trigger(highlighted) and its origin point.

42385-spawn2.png

I’ve added this sphere to show where the objects are now spawning from(y axis)

Maybe you should set the spawnPosition relative to the transform’s position.

Vector3 spawnPosition = transform.position + new Vector3 (Random.Range (-spawnVariation, spawnVariation), 0, 0);

did you try Vector3 spawnPosition = new Vector3(Random.Range(-spawnVariation, spawnVariation),0 , transform.position.z) ?

i have the same type of issue :frowning:

this is the link of the video which i’m facing right now…