简体   繁体   中英

How to change a scene in unity when a certain amount of buttons are clicked

I am working on a memory matching game in Unity where the user has to memorize 3 letters and then click 3 image buttons with those letters on them. If the correct image buttons are clicked, it changes to the level complete scene, if not it goes to a game over scene. How to I go about changing the scene if the 3 correct buttons are clicked?

I'm new to Unity so any links/code snippets would be amazing!

If the player has to memorize 3 letters and then press 3 buttons with these letters, I assume there are going to be more buttons with wrong letters (otherwise it is impossible to loose).

I assume you know how to create buttons in the Canvas. So you pass a different function for each letter which will set to true a flag for that letter and increase a counter.

void OnClickA()
{
    counterLetter++;
    FlagA = true;
    //or an array which is position is one leter index = 0 -> a, index = 1 -> b
    ArrayLetters[0] = true
}

Then you check if(counterLetter == 3) and you check if the flags you were expecting are true. You can implement this as you prefer. Checking the flags or checking if the correct positions of the array are set to 1.

In case the player selected the correct button, (And I think here is where starts the part you didn´t know how to solve) loads the victory level:

Application.LoadLevel("VictoryScene");

In case he made a mistake:

Application.LoadLevel("LostScene");

Edit based on @ZayedUpal´s comment.For new versions of Unity (later than5.3), you should use intead:

UnityEngine.SceneManagement;
SceneManager.LoadScene("nameOfScene");

To be able to load this scenes, first you need to create them in the Editor, then you need to add them to the build.

File -> Build Settings

There you drag and drop the scenes you want to build in the top window.

在此处输入图片说明

So later you will be able to pass their names to the function LoadLevel()

Changing scenes is pretty straight forward in Unity:

using UnityEngine.SceneManagement;
SceneManager.LoadScene("OtherSceneName");

You can also use the scene build index found in the build menu, although I don't recommend it because it might change if your project gets bigger:

SceneManager.LoadScene(1);

Unity docs on this method: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM