Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Error CS0161 (32,24) and Error CS0103 (37,12)

Discussion in 'Getting Started' started by edward19675, Feb 22, 2024.

  1. edward19675

    edward19675

    Joined:
    Feb 13, 2024
    Posts:
    5
    when i was programming i got these two Error, and i dont know how to fix it.


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

    public class NewBehaviourScript : MonoBehaviour
    {
    public GameObject player;
    public float TimeBeforeNextScene;
    public bool PlayerIsAtTheDoor;
    private BoxCollider2D coll;


    // Start is called before the first frame update
    void Start()
    {
    coll = GetComponent<BoxCollider2D>();
    }

    // Update is called once per frame
    void Update()
    {
    if(Input.GetKeyDown(KeyCode.E) && PlayerIsAtTheDoor == true)
    {
    StartCoroutine(_OpenDoor());
    }
    }


    public IEnumerator _OpenDoor()
    {
    SceneManager.LoadScene("Scene 2");
    }

    private void OnTriggerEnter2D(Collider2D coll)
    {
    if(collision.gameObject.tag =="player")
    {
    PlayerIsAtTheDoor = true;
    }
    }

    private void OnTriggerExit2D(Collider2D coll)
    {
    PlayerIsAtTheDoor = false;
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,066
    You can fix your own typing mistakes. Here's how:

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    bugfinders likes this.
  3. edward19675

    edward19675

    Joined:
    Feb 13, 2024
    Posts:
    5
    i still dont know how to fix Error CS0161 (32,24). It says not all code paths return a value.

    this line

    public IEnumerator _OpenDoor()
    {
    SceneManager.LoadScene("Scene 2");
    }
     
    Last edited: Feb 22, 2024
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,214
    You will want to familiarise yourself with how to actually write coroutines: https://docs.unity3d.com/Manual/Coroutines.html

    There are examples of how to properly load scenes in the documentation too: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html
     
  5. edward19675

    edward19675

    Joined:
    Feb 13, 2024
    Posts:
    5
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,214
    Then read the links I shared and they will show you how to do so.
     
    Kurt-Dekker likes this.