Search Unity

Question Game Keeps Crashing and I Have No Idea Why

Discussion in 'Visual Scripting' started by BubbleGobbler, Oct 30, 2022.

  1. BubbleGobbler

    BubbleGobbler

    Joined:
    Aug 23, 2022
    Posts:
    7
    Hello, I'm here with a question about my game with a simple first-person controller that keeps crashing. When I hit play, the whole scene crashes and I can't interact with anything nor can I close the window. I'm pretty new, so I have no idea what is causing the issue. My computer is working fine and has enough RAM, so I think the issue lies within one of my scripts. I noticed that the crashing happened after I made changes to one of my scripts. What I did was include a mechanic where if the player presses w to move forward and left shift, their speed gradually increases until it reaches a maximum speed.
    This issue has been plaguing me for a while and is preventing me from working on the game at all. I would really appreciate any help I can receive. Thank you!


    The code:

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

    //include a way to run, crouch, and a way to jump

    public class PlayerMovement : MonoBehaviour
    {
    [SerializeField]
    private CharacterController character_Controller;
    public Collider playerBase;
    Vector3 gravitationalforce;
    public float speed = 12f;
    float gravity = 0f;

    // Start is called before the first frame update
    void Start()
    {
    character_Controller = GetComponent<CharacterController>();
    playerBase = GetComponent<Collider>();
    StartCoroutine(Running());
    }

    // Update is called once per frame
    void Update()
    {
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");
    Vector3 move = transform.right * x + transform.forward * z;
    character_Controller.Move(move * speed * Time.deltaTime);

    gravitationalforce.y += gravity * Time.deltaTime;
    character_Controller.Move(gravitationalforce * Time.deltaTime);

    if (speed > 24)
    {
    speed = 24;
    }

    if (!Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
    {
    speed = 12f;
    }
    }

    private void OnTriggerEnter(Collider collision)
    {
    {
    if (collision.gameObject.CompareTag("ThePlayer"))
    {
    gravity = 0;
    }
    }
    }

    IEnumerator Running()
    {
    while (true)
    if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
    {
    speed += 1f;
    yield return new WaitForSeconds(0.2f);
    yield return null;
    }
    }
    }[/code]
     
  2. BubbleGobbler

    BubbleGobbler

    Joined:
    Aug 23, 2022
    Posts:
    7
    The issue might not be related to the script even though I suspect it is, so you can try asking for more details. I will readily and gladly provide them.
     
  3. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    396
    while (true) is an infinite loop, the only time it goes to the next frame is when you press shift or W. What you need to do is put the If input.getkey shift+w in the update loop, then If canSpeedUp == true (make a bool), then StartCoroutine(Running) and set canSpeedUp = false, wait 0.2 seconds, then canSpeedUp = true. Add an else to If input.get, speed = 12f.
     
    BubbleGobbler likes this.
  4. BubbleGobbler

    BubbleGobbler

    Joined:
    Aug 23, 2022
    Posts:
    7
    Hello, thank you for your response. I prevented the game from crashing and improved my running mechanic system by changing my code. Therefore, I resolved my issues and no longer need this thread anymore.
     
    Trindenberg likes this.