Multiple spawners in scene are all spawning their item to one instance of spawner
Hey guys, feel I am doing something very foolish and just not realizing it.
I have 8 spawner prefabs in a scene from a default spawner prefab I made. I am using these to spawn a barrel prefab at their transform position, then move the barrel along to an endpoint, which is included in the default prefab, so is the same local distance and direction in all instanced cases. In the case below, an item should appear behind the spawner, slide along the flat section, then stop before the curve, kind of like a conveyor belt.
When I run the game with a single spawner, I have no issues, but as soon as I add multiple spawners, all items spawn onto a single spawner, but with their inherited orientations. I feel this definitely has something to do with how I am using the prefabs so any advice would be appreciated, but would be interested to know what I am doing wrong none the less.
public class CargoSpawner : MonoBehaviour
{
public GameObject cargo;
public Transform endPoint;
void Start()
{
SpawnNew();
}
void SpawnNew()
{
Instantiate(cargo,transform.position,transform.rotation);
cargo.GetComponent<CargoOnSpawn>().SetTransforms(transform, endPoint);
}
}
//adapted from unity doc lerp example
public class CargoOnSpawn : MonoBehaviour
{
private static bool spawned = false;
private static bool finished = false;
private static Transform startPoint;
private static Transform endPoint;
public static float speed = 1f;
private static float startTime;
private static float journeyLength;
//if spawned correctly, lerp along path and check if at destination, if so, stop
void Update()
{
if (spawned)
{
MoveIntoScene();
if (transform.position == endPoint.position) finished = true;
}else Debug.Log("Not Spawned");
}
void MoveIntoScene()
{
float distCovered = (Time.time - startTime) * speed;
float fracJourney = (float)distCovered / (float)journeyLength;
transform.position = Vector3.Lerp(startPoint.position, endPoint.position, fracJourney);
}
public void SetTransforms(Transform start, Transform end)
{
startPoint = start;
endPoint = end;
startTime = Time.time;
journeyLength = Vector3.Distance(startPoint.position, endPoint.position);
spawned = true;
}
}