I'm trying to instantiate a new gameObject when the current gameObject gets to a select point. The problem comes when the program gets to the second if() statement. It sets the floorsSpawned to 1 and when it does, the game object instantiates a bunch of times. Shouldn't the floorSpawned be set to 2 in the first if() statement and only instantiated once?
public class FloorMovement : MonoBehaviour
{
public GameObject floor;
[SerializeField] int speed = 10;
public int floorsSpawned = 1;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector2.left * Time.deltaTime * speed);
if (transform.position.x <= -18.0 && floorsSpawned == 1)
{
floorsSpawned = 2;
Instantiate(floor, new Vector2(11.5f, -2.0f), Quaternion.identity);
}
if (transform.position.x <= -40.25 && floorsSpawned == 2)
{
floorsSpawned = 1;
Destroy(floor, 3);
}
}
}