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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unity crashing with my scripts

Discussion in 'Scripting' started by jj242821, Jan 16, 2020.

  1. jj242821

    jj242821

    Joined:
    Mar 24, 2013
    Posts:
    1
    Hi guys, after many years of not really putting in the effort. I'm now trying to really learn unity and c#.

    I followed brackeys with a player movement controller and it worked great. I created another scripts for my player stats. I'm trying to make it so that when my character sprints, the stamina drains. But unity crashes every time I make the character sprint. It was doing the same thing before I ever connected the scripts. Any idea what the issue is? I can't seem to find any answers anywhere else online. Thank you in advance.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerMovement : MonoBehaviour
    6. {
    7.     public player player;
    8.     public CharacterController controller;
    9.  
    10.     public float sprinting = 1.8f;
    11.     public float speed = 12f;
    12.     public float gravity = -9.81f;
    13.     public float jumpHeight = 3f;
    14.  
    15.     public Transform groundCheck;
    16.     public float groundDistance = 0.4f;
    17.     public LayerMask groundMask;
    18.  
    19.     Vector3 velocity;
    20.     bool isGrounded;
    21.     public bool isSprinting;
    22.    
    23.  
    24.  
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    30.  
    31.         if(isGrounded && velocity.y < 0)
    32.         {
    33.             velocity.y = -2f;
    34.         }
    35.  
    36.         float x = Input.GetAxis("Horizontal");
    37.         float z = Input.GetAxis("Vertical");
    38.  
    39.  
    40.         if (Input.GetKeyDown("left shift"))
    41.         {
    42.             isSprinting = true;
    43.             speed = speed * sprinting;
    44.         }
    45.         if (Input.GetKeyUp("left shift"))
    46.         {
    47.             isSprinting = false;
    48.             speed = speed / sprinting;
    49.         }
    50.         while(isSprinting == true)
    51.         {
    52.             player.playerStamina -= 5f * Time.deltaTime;
    53.         }
    54.  
    55.         Vector3 move = transform.right * x + transform.forward * z;
    56.  
    57.         controller.Move(move * speed * Time.deltaTime);
    58.  
    59.         if(Input.GetButtonDown("Jump") && isGrounded)
    60.         {
    61.             velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    62.         }
    63.  
    64.         velocity.y += gravity * Time.deltaTime;
    65.  
    66.         controller.Move(velocity * Time.deltaTime);
    67.     }
    68. }
    and the player scripts is
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class player : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public float playerHealth = 100f;
    10.     public float playerStamina = 100f;
    11.  
    12.  
    13.     private void Start()
    14.     {
    15.      
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (playerHealth <= 0)
    21.         {
    22.             Destroy(gameObject);
    23.         }
    24.  
    25.      
    26.  
    27.     }
    28.  
    29.  
    30. }
    31.  
     
    Last edited: Jan 16, 2020
  2. PaulR

    PaulR

    Joined:
    Nov 14, 2012
    Posts:
    43
    Code (CSharp):
    1.         if (Input.GetKeyDown("left shift"))
    2.         {
    3.             isSprinting = true;
    4.             speed = speed * sprinting;
    5.         }
    6.         if (Input.GetKeyUp("left shift"))
    7.         {
    8.             isSprinting = false;
    9.             speed = speed / sprinting;
    10.         }
    11.         while(isSprinting == true)
    12.         {
    13.             player.playerStamina -= 5f * Time.deltaTime;
    14.         }
    You have an infinite loop here. Once isSprinting is set, Update() will never return. Just remove the while(isSprinting) condition.
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    As @PaulR notes above, you can never busy loop like that in Unity. That actually is your lockup. Nothing else happens while you are doing your thing, so unless you manually cleared
    isSprinting
    inside the loop, the results of subtracting from
    playerStamina
    will never be observed by any other code.

    If you need to do such a construct, the only permissible way is to make it a coroutine started with
    StartCoroutine()
    and ALSO
    yield
    periodically inside that loop.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    Since Update runs every frame, it may work simply by replacing "while" with "if" or something like that.

    Code (CSharp):
    1.        if (isSprinting == true)
    2.         {
    3.             player.playerStamina -= 5f * Time.deltaTime;
    4.         }