Search Unity

Player not spawning

Discussion in 'Scripting' started by greeksurvivor, Jun 17, 2018.

  1. greeksurvivor

    greeksurvivor

    Joined:
    Jun 17, 2018
    Posts:
    5
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float moveSpeed;
    8.  
    9.     private Animator anim;
    10.     private Rigidbody2D myRigidBody;
    11.  
    12.     private bool playerMoving;
    13.     private Vector2 lastMove;
    14.  
    15.     private static bool playerExists;
    16.  
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         anim = GetComponent<Animator>();
    21.         myRigidBody = GetComponent<Rigidbody2D>();
    22.             if (!playerExists)
    23.             {
    24.                 playerExists = true;
    25.                 DontDestroyOnLoad(transform.gameObject);
    26.             }
    27.             else
    28.             Destroy(gameObject);
    29.     }
    30.    
    31.     // Update is called once per frame
    32.     void Update () {
    33.         playerMoving = false;
    34.         if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
    35.         {
    36.             //transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
    37.             myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, Input.GetAxisRaw("Vertical") * moveSpeed);
    38.             playerMoving = true;
    39.             lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
    40.         }
    41.  
    42.         if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
    43.         {
    44.             //transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime,0f,0f));
    45.             myRigidBody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, myRigidBody.velocity.y);
    46.             playerMoving = true;
    47.             lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    48.         }
    49.         if(Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
    50.         {
    51.             myRigidBody.velocity = new Vector2(0f, myRigidBody.velocity.y);
    52.         }
    53.         if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
    54.         {
    55.             myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, 0f);
    56.         }
    57.  
    58.         anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
    59.         anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
    60.         anim.SetBool("PlayerMoving", playerMoving);
    61.         anim.SetFloat("LastMoveX", lastMove.x);
    62.         anim.SetFloat("LastMoveY", lastMove.y);
    63.     }
    64. }
    65.  
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    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.
     
  3. greeksurvivor

    greeksurvivor

    Joined:
    Jun 17, 2018
    Posts:
    5
    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.
     
    Last edited: Jun 17, 2018
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    So, assuming you were running through in the debugger, there is no PlayerController in your scene (or at least not an active one).
     
  5. greeksurvivor

    greeksurvivor

    Joined:
    Jun 17, 2018
    Posts:
    5
    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
     
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    In that case, you have 2 PlayerController's in the scene. Can you remove one of them?
     
  7. greeksurvivor

    greeksurvivor

    Joined:
    Jun 17, 2018
    Posts:
    5
    Yeah, that was it, i had accidentally assigned 2 scripts on the player!

    Thanks for the help!!
     
    Doug_B likes this.