# Using points to unlock next scene in Unity

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);

}
}


• 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. – Vaillancourt Oct 10 at 0:38
• 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. – Pikalek Oct 10 at 3:40

## 1 Answer

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...

• 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' – celine Oct 10 at 6:02
• oh yeah i can see it. i will update the script and then use it. the error is from 'if statement'. – Swati Oct 10 at 6:04
• 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. – Swati Oct 10 at 6:06
• there is another issue that i forget about... is : const int ScorePrefix = "Score:"; – Swati Oct 10 at 6:09
• @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. – Bilkokuya Oct 10 at 10:08