Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Viwo04 · Apr 23 at 10:15 AM · scenes

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


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chubzdoomer · Apr 24 at 09:42 PM 1
Share

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.

avatar image Captain_Pineapple Chubzdoomer · Apr 25 at 06:41 AM 0
Share

What @Chubzdoomer says

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges