1
\$\begingroup\$

Below is my code and I'm a beginning game designer trying to make my first game for art school teehee! :)

I'm not sure how to unlock the next level with a scene I've already made called Rust_Room

Any ideas? :o I also attached the error codes I'm getting!

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

public class Example
{
    HUD hud;
    int points;

    const int ScorePrefix = "Score:";
    int score;

    public void LoadLevel()
    {
        if ("Score==45")
        // load the nextlevel
        SceneManager.LoadScene(SceneManager.GetSceneByName(Rust_Room)().buildIndex + 1);

    }
}

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ If you have to update your post, please use the edit feature, which can be seen at the bottom of your question, instead of creating a new question. \$\endgroup\$ – Vaillancourt Oct 10 at 0:38
  • \$\begingroup\$ Welcome to GDSE. Whenever possible it's preferable to post error messages as text - it makes it easier to answer & also helps others who might be searching for solutions to the same or similar errors. \$\endgroup\$ – Pikalek Oct 10 at 3:40
1
\$\begingroup\$

There are very clear issues with your script as in the error mentioned:

"Cannot implicitly convert type string to bool"

And this error has been generated by the "if" statement as it needed bool not a string, to fix it just remove the double quotes like this:

if (Score == 45)

also the same error has been casued by:

const int ScorePrefix = "Score:";

so you can remove this variable as it not used in this script or you can assign an int value to it. int is basically a number with no decimal point for example 1,2,10,489 etc. To read more about C# data types please visit here. or Here.

const int ScorePrefix = 10; //or any value which is int.*

Error # 2:

"The name 'Rust_Room' does not exist in the current context"

this is generated by this:

SceneManager.LoadScene(SceneManager.GetSceneByName(Rust_Room)().buildIndex + 1);

as you can see you are passing 'Rust_Room' is a 'type' while it needs name of the scene which is string.

So jut do it like this and it resolve the issue:

SceneManager.LoadScene(SceneManager.GetSceneByName("Rust_Room")().buildIndex + 1);

So the final script will be:

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

public class Example
{
    HUD hud;
    int points;

    const int ScorePrefix = 10; //or any value which is int.
    int score;

    public void LoadLevel()
    {
        if (Score == 45)
        {
             // load the nextlevel
             SceneManager.LoadScene(SceneManager.GetSceneByName("Rust_Room")().buildIndex + 1);
        }

    }
}

By the way i will handle this situation using build index instead of name here is how i will do it:

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

public class Example
{
    HUD hud;
    int points;

    const int ScorePrefix = 10; //or any value which is int.
    int score;

    public void LoadLevel()
    {
        if (Score==45)
        {
            int currentScene = SceneManager.GetActiveScene().buildIndex + 1;
            SceneManager.LoadScene(currentScene);
        }
    }
}

In this code i first get current scene index and then increment by 1 to get next scene index and finally loading scene with incremented index.

I hope this will help...

\$\endgroup\$
  • \$\begingroup\$ thanks for helping Swati!! I used your script that uses build index instead of name and ended up getting the error message: Assets/scripts/RustRoom.cs(10,26): error CS0029: Cannot implicitly convert type 'string' to 'int' \$\endgroup\$ – celine Oct 10 at 6:02
  • \$\begingroup\$ oh yeah i can see it. i will update the script and then use it. the error is from 'if statement'. \$\endgroup\$ – Swati Oct 10 at 6:04
  • \$\begingroup\$ I've update the script now you can use it. or just remove the quotes from the 'if' statement and that will do the trick. If my answer helps then consider up vote and accept my answer. \$\endgroup\$ – Swati Oct 10 at 6:06
  • \$\begingroup\$ there is another issue that i forget about... is : const int ScorePrefix = "Score:"; \$\endgroup\$ – Swati Oct 10 at 6:09
  • 1
    \$\begingroup\$ @celine Just for future; if you see that error message "Cannot implicitly convert type 'string' to 'int' " - it means you have given the wrong type of variable to an expression. In this case, you tried to pass a "string" to an expression that requires an "int" variable. If you're unsure, it is often also easiest just to google the specific error message you get (from the "error CSxxxx:" part onwards. Or even just google the specific number "CSxxxx") - most of these are common errors that have good explanations online. \$\endgroup\$ – Bilkokuya Oct 10 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.