- Home /
How to code a script to go to the next scene after winning a round?
what to do to move to the next scene when the player wins the round and not when the mouse button is pressed. I am mainly referring to this passage This is my code for scene switching:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelLoader : MonoBehaviour
{
public Animator transition;
public float transitionTime = 1f;
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtDown(0))
{
LoadNextLevel();
}
}
public void LoadNextLevel()
{
StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex + 1));
}
IEnumerator LoadLevel(int levelIndex)
{
transition.SetTrigger("Start");
yield return new WaitForSeconds(transitionTime);
SceneManager.LoadScene(levelIndex);
}
}
And this is my code that controls the whole game:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(GameUI))]
public class GameController : MonoBehaviour
{
public static GameController Instance { get; private set; }
[SerializeField]
private int knifeCount;
[Header("Knife Spawning")]
[SerializeField]
private Vector2 knifeSpawnPosition;
[SerializeField]
private GameObject knifeObject;
public GameUI GameUI { get; private set; }
private void Awake()
{
Instance = this;
GameUI = GetComponent<GameUI>();
}
private void Start()
{
GameUI.SetInitialDisplayedKnifeCount(knifeCount);
SpawnKnife();
}
public void OnSuccessfulKnifeHit()
{
if (knifeCount > 0)
{
SpawnKnife();
}
else
{
StartGameOverSequence(true);
}
}
private void SpawnKnife()
{
knifeCount--;
Instantiate(knifeObject, knifeSpawnPosition, Quaternion.identity);
}
public void StartGameOverSequence(bool win)
{
StartCoroutine("GameOverSequenceCoroutine", win);
}
private IEnumerator GameOverSequenceCoroutine(bool win)
{
if (win)
{
yield return new WaitForSecondsRealtime(0.3f);
}
else
{
GameUI.ShowRestartButton();
}
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
}
}
Answer by Captain_Pineapple · Apr 23 at 02:26 PM
Given the code you already created i don't really get the question. You simply need a reference to your LevelLoader
instance. Either by assigning it to a member variable in your GameController or by adding a static Instance reference to your levelLoader or by FindObjectOfType.
Then you just have to call LoadNextLevel()
right?
Please add more details to your question if you need more specific help with something.
Answer by Viwo04 · Apr 24 at 11:42 AM
do you mean it?
if (win)
{
FindObjectOfType<LevelLoader>.LoadNextLevel();
yield return new WaitForSecondsRealtime(0.3f);
}
because it doesn't work
When you say it "doesn't work," what does that mean? Does anything happen at all? Are you seeing any error messages in the console? The more details you provide, the better.
The code you posted honestly shouldn't even compile, and you should be seeing red squiggly lines in the editor (provided you've paired it correctly with Unity). The reason I say this is that you've forgotten the parentheses after <LevelLoader>
. The correct syntax is as follows:
FindObjectOfType<LevelLoader>().LoadNextLevel();
If you aren't seeing red squiggly lines, then you should open Unity and go to Edit->Preferences->External Tools and change External Script Editor to be the IDE you're currently using (presumably Visual Studio). This will make your life 10x easier as a programmer.
Your answer
Follow this Question
Related Questions
switching scenes 1 Answer
Load specific scene in editor 2 Answers
Application.LoadLevel loading the current scene instead of the specifed one 1 Answer
Collision Help Please 1 Answer
Why won't my scene load? 1 Answer