Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Character duplication

Discussion in '2D' started by erymael17, Mar 5, 2021.

  1. erymael17

    erymael17

    Joined:
    Feb 8, 2021
    Posts:
    6
    Hi I'm new to Unity(literally I started about 1 month ago) I'm having a problem each time my character goes from one stage to another it duplicates. What can I do to solve this problem?? Thanks for any help!!
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Are you instantiating the player every time a scene changes? Are you also using a prefab to player the player in each scene manually? Basically, you want to do one or the other, not both. If that isnt the solution show your code (with code tags from the ribbon please) where you spawn in the player.
     
  3. erymael17

    erymael17

    Joined:
    Feb 8, 2021
    Posts:
    6
    Ok so this is the code that the player has


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

    public class PlayerController : MonoBehaviour
    {
    private float movementInputDirection;

    private bool isFacingRight = true;

    private Rigidbody2D rb;
    private GameObject[] players;

    public float movementSpeed = 10f;
    public float jumpForce = 16.0f;

    // Start is called before the first frame update
    void Start()
    {
    DontDestroyOnLoad(gameObject);
    rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
    CheckInput();
    CheckMovementDirection();
    }

    private void FixedUpdate()
    {
    ApplyMovement();
    }

    private void CheckMovementDirection()
    {
    if(isFacingRight && movementInputDirection < 0)
    {
    Flip();
    }
    else if (!isFacingRight && movementInputDirection > 0)
    {
    Flip();
    }
    }


    private void CheckInput()
    {
    movementInputDirection = Input.GetAxisRaw("Horizontal");

    if (Input.GetButtonDown("Jump"))
    {
    Jump();
    }
    }

    private void OnLevelWasLoaded(int level)
    {
    FindStartPos();

    players = GameObject.FindGameObjectsWithTag("Player");

    }

    private void Jump()
    {
    rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    }

    private void ApplyMovement()
    {
    rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
    }

    private void Flip()
    {
    isFacingRight = !isFacingRight;
    transform.Rotate(0.0f, 180.0f, 0.0f);
    }

    private void FindStartPos()
    {
    transform.position = GameObject.FindWithTag("StartPos").transform.position;
    }
    }


    And this is the code I used to move from one stage to the other


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

    public class endtrigger : MonoBehaviour
    {
    [SerializeField] private string newlevel;

    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.CompareTag("Player"))
    {
    SceneManager.LoadScene(newlevel);
    }

    }
    }
     
  4. erymael17

    erymael17

    Joined:
    Feb 8, 2021
    Posts:
    6
    Nevermind already fix it!!! :)
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Glad you fixed it but you didnt use code tags for posting your code. Please do that going forward, it will enable others to help you much better.

    upload_2021-3-5_14-26-52.png
     
  6. erymael17

    erymael17

    Joined:
    Feb 8, 2021
    Posts:
    6
    Thanks for the tip!!
     
    Cornysam likes this.