I have written the code as you can see bellow and if i put the Destroy(gameObject) after the else statement, the player won't load at the screen at all, but if i remove it the player will spawn, but then he is going to be duplicated at least one time. Can anyone help? Thanks in advance! Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed; private Animator anim; private Rigidbody2D myRigidBody; private bool playerMoving; private Vector2 lastMove; private static bool playerExists; // Use this for initialization void Start () { anim = GetComponent<Animator>(); myRigidBody = GetComponent<Rigidbody2D>(); if (!playerExists) { playerExists = true; DontDestroyOnLoad(transform.gameObject); } else Destroy(gameObject); } // Update is called once per frame void Update () { playerMoving = false; if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f) { //transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f)); myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, Input.GetAxisRaw("Vertical") * moveSpeed); playerMoving = true; lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical")); } if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f) { //transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime,0f,0f)); myRigidBody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, myRigidBody.velocity.y); playerMoving = true; lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f); } if(Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f) { myRigidBody.velocity = new Vector2(0f, myRigidBody.velocity.y); } if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f) { myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, 0f); } anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal")); anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical")); anim.SetBool("PlayerMoving", playerMoving); anim.SetFloat("LastMoveX", lastMove.x); anim.SetFloat("LastMoveY", lastMove.y); } }
That would make sense, if the Start() method of an object destroys itself then it doesn't stand a chance to survive. Is this happening after a new scene load or in just a single scene? I think your best bet is to set a breakpoint at line 22 and step it through. That will help shed some light on to what's happening.
The problem is that is happening in the first scene. So if I understand correctly you suggest to put a break just before the if statement correct ? EDIT: I just added a breaking point but nothing happened.
So, assuming you were running through in the debugger, there is no PlayerController in your scene (or at least not an active one).
I just figured out that although it just in the start void the if statement is being checked 2 times so the second time the player is being deleted because the !playerExists isn't true