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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Argument 1: cannot convert from 'void' to 'int'

Discussion in 'Scripting' started by SharkY055, Dec 21, 2020.

  1. SharkY055

    SharkY055

    Joined:
    Sep 28, 2019
    Posts:
    4
    Greetings, I want to create an animation between some of my scenes, I followed the tutorial from Brackeys and wanted to do some things differently. I'm getting the error in the title for some reason in 44,34, can anyone please help me? Thank you for your time!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class LoadNewLevel : MonoBehaviour
    7. {
    8.     public bool playerInRange;
    9.  
    10.     public int load_level;
    11.     public Animator transition;
    12.     public float transitionTime = 1f;
    13.     public string level_name;
    14.     // Start is called before the first frame update
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if(Input.GetKeyDown(KeyCode.E) && playerInRange)
    20.         {
    21.  
    22.             LoadNextLevel();
    23.         }
    24.     }
    25.  
    26.     void OnTriggerEnter2D(Collider2D other)
    27.     {
    28.         if(other.CompareTag("Player"))
    29.         {
    30.             playerInRange = true;
    31.         }
    32.     }
    33.  
    34.     void OnTriggerExit2D(Collider2D other)
    35.     {
    36.         if(other.CompareTag("Player"))
    37.         {
    38.             playerInRange = false;
    39.         }
    40.     }
    41.  
    42.     public void LoadNextLevel()
    43.     {
    44.         StartCoroutine(LoadLevel(SceneManager.LoadScene(level_name)));
    45.     }
    46.  
    47.     IEnumerator LoadLevel(int levelIndex)
    48.     {
    49.          transition.SetTrigger("Start");
    50.          yield return new WaitForSeconds(1);
    51.          SceneManager.LoadScene(levelIndex);
    52.  
    53.     }
    54.  
    55. }
    56.  
     
    Last edited: Dec 21, 2020
  2. MatrixQ

    MatrixQ

    Joined:
    May 16, 2020
    Posts:
    87
    Scenemanager.LoadScene is a method that doesn't return a value, so its return type is "void". LoadLevel() expects there to be an int, but you are giving it a void. You need to put an integer value where "SceneManager.LoadScene(level_name)" is written. I guess you'll need some way to translate your level_name to a level_index.
     
  3. SharkY055

    SharkY055

    Joined:
    Sep 28, 2019
    Posts:
    4
    Thank you very much for your response, I'll try to find a way to do this :) !
     
  4. MatrixQ

    MatrixQ

    Joined:
    May 16, 2020
    Posts:
    87
    The Scenemanager can also load scenes from name. But to do so in your case, you would need to change your LoadLevel() Enumerator to accept a string with the name.
     
  5. SharkY055

    SharkY055

    Joined:
    Sep 28, 2019
    Posts:
    4
    So I would need to change things inside the IEnumerator? Can I change the levelIndex int to a string, or that wouldn't work either?
     
  6. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    Yeah, if you change the parameter of LoadLevel to a "string" type, and remove the SceneManager.LoadScene part of line 44, it should work - just call StartCoroutine(LoadLevel(level_name)) there.
     
    Aezthetics and SharkY055 like this.
  7. SharkY055

    SharkY055

    Joined:
    Sep 28, 2019
    Posts:
    4
    Thank you very very much, I managed to do it :-D !!! Thank you so much, I wish you happy holidays :-D !!
     
  8. razorianbuck

    razorianbuck

    Joined:
    Feb 13, 2021
    Posts:
    1
  9. Aezthetics

    Aezthetics

    Joined:
    Aug 19, 2022
    Posts:
    1
    Thank you, this works!