Home > Software design >  Unity: When I instantiate my character, it instantiates twice
Unity: When I instantiate my character, it instantiates twice

Time:01-25

I'm working on a platformer game, and I instantiate my character after he gets hit by an enemy object. The problem is, my character is instantiating twice. Here's my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerMove : MonoBehaviour
{
    public GameObject player;
    private Rigidbody2D rb;
    private SpriteRenderer sprite;
    private BoxCollider2D coll;
    private Animator anim;

    public float Speed;
    public float JumpForce;
    private float dirX = 0f;

    private float fJumpPressedRemember = 0f;
    private float fJumpPressedRememberTime = 0.2f;

    [SerializeField] private LayerMask jumpableGround;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        sprite = GetComponent<SpriteRenderer>();
        coll = GetComponent<BoxCollider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        dirX = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(dirX * Speed, rb.velocity.y);

        fJumpPressedRemember -= Time.deltaTime;
        if (Input.GetButtonDown("Jump"))
        {
            fJumpPressedRemember = fJumpPressedRememberTime;
        }

        if ((fJumpPressedRemember > 0) && isGrounded())
        {
            fJumpPressedRemember = 0;
            rb.velocity = new Vector2(rb.velocity.x, JumpForce);
        }

        UpdateAnimation();
    }
    private bool isGrounded()
    {
        return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    }

    void OnCollisionEnter2D(Collision2D spikeCol)
    {
        if (spikeCol.gameObject.tag.Equals("DamageDealer") == true)
        {
            Instantiate(player, new Vector3(-10, 1, 0), Quaternion.identity);
            Destroy(player);
        }

        if (spikeCol.gameObject.tag.Equals("End") == true)
        {
            SceneManager.LoadScene("Level2", LoadSceneMode.Single);
        }
        
        if (spikeCol.gameObject.tag.Equals("End2") == true)
        {
            SceneManager.LoadScene("Level1", LoadSceneMode.Single);
        }
    }

    private void UpdateAnimation()
    {
        if (dirX > 0f)
        {
            anim.SetBool("running", true);
            sprite.flipX = false;
        }
        else if (dirX < 0f)
        {
            anim.SetBool("running", true);
            sprite.flipX = true;
        }
        else
        {
            anim.SetBool("running", false);
        }
    }

}

It's supposed to instantiate once after dying. I don't know what's happening, but it only started after I added animation code, so I think that's the issue. Thanks to anyone who can help!

CodePudding user response:

OnCollisionEnter2D is called twice. Reasons why this can be described here: Why is OnCollisionEnter getting called twice?

You can add instantiate method double call check, like this:

private bool isInited;

void OnCollisionEnter2D(Collision2D spikeCol)
{
    if (spikeCol.gameObject.tag.Equals("DamageDealer") == true)
    {
        if (isInited)
        {
            return;
        }

        isInited = true;
        Instantiate(player, new Vector3(-10, 1, 0), Quaternion.identity);
        Destroy(player);
    }
    ...

}
  • Related