0
\$\begingroup\$

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);
        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Where is the game object when you call the script (what is its x coordinate)? \$\endgroup\$ – Alex F Jun 27 at 0:47
  • \$\begingroup\$ Did you try to step-by-step debug, or write a bunch of messages to the console using Debug.Log to see what's going on? Your programmer life will be much easier when you'll know your basic debugging tools. \$\endgroup\$ – Alexandre Vaillancourt Jun 27 at 2:01
  • \$\begingroup\$ Is this behavior attached to multiple objects (like say the newly-spawned floors?) \$\endgroup\$ – A C Jun 27 at 7:13
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.