TransWikia.com

My GameObject keeps spawning when it shouldn't

Game Development Asked by Michael Hampshire on November 9, 2021

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);
        }
    }
}

2 Answers

If transform.position.x is lower than -40.25, on the same frame you are instantiating and destroying the gameobject. First not solution: Use and else-if for avoiding do the 2 things at the same frame.

Second thing: Why the object is not being destroyed at the second if. You are destroying the non-instantiated prefab gameobject, not the instantiated one 3 instruction lines above. Cache the instantiated gameobject somewhere for destroying it later.

Anyway, with that 2 "solutions" in one frame you will create the go and at the next frame you will send the Destroy instruction to that go, and the engine will destroy it when it wants (its destroy, not immediatedestroy).

There is no real solution, you will have to rethink that method.

Answered by Undume on November 9, 2021

What happens:

  1. Your object state is (x = -10, floorsSpawned = 1)
  2. You object moves to the limit of the first if (state: x = -18.1, floorsSpawned = 1) and the first object is instantiated (floorsSpawned becomes 2)
  3. You object keeps moving to reach the second if (state: x = -40.26, floorsSpawned = 2), the object gets destroyed and floorsSpawned becomes 1

Now your problem on the next frame(s)

x is still smaller than the value in the first if ( x <= -18.0) and since floorsSpawned = 1 the object gets created and floorsSpawned is set to 2

On the next frame you are most likely smaller than the value in the second if (x <= 40.25)....

and so it repeats

The solution depends on the desired behaviour and how the object moves (can it move only along -x or both -/+ x)

  • introduce ranges in the ifs (e.g.)

if (transform.position.x <= -18.0 && transform.position.x > -40.25 && floorsSpawned == 1)

  • if it shouldn't be spawned any more (so only once) you could set floorsSpawned to 3 in the second if

Answered by monty on November 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP