Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by S_Byrnes · May 12, 2016 at 08:33 AM · c#sceneloadbooleanrefresh

Can anyone tell me what the issue is with my code? (C#)

Hi guys,

I'm having a problem figuring out what exactly is wrong with my script. Basically what I need to do is have it so after the player receives points, that it will load the next stage (a scene number that I've put in the inspector).

All it does now though is refreshes the current level and does not give it time to give the player the points or even show that the player earned them before it makes a dash to refreshing it.

I'll just post the code and hopefully you'll be able to see the mistakes that I can't.

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
     public int sceneNumber = 0;
 
     bool Moving;
     bool halfMoving;
 
     //bool Goal = false;
     bool Outer = false;
     bool Middle = false;
     bool Center = false;
     bool Finished = false;
     bool Ended = false;
 
     void Update()
     {
 
         if (!halfMoving && Outer)
         {
             Finished = true;
             Outer = false;
             ManageScore.score += outerPoints;
             Ended = true;
         }
 
         if (!halfMoving && Middle)
         {
             Finished = true;
             Middle = false;
             ManageScore.score += middlePoints;
             Ended = true;
         }
 
         if (!halfMoving && Center)
         {
             Finished = true;
             Center = false;
             ManageScore.score += centerPoints;
             Ended = true;
         }
 
         if (Ended == true)
         {
             SceneManager.LoadScene(sceneNumber, LoadSceneMode.Single);
         }
     }
 }

Thanks for reading this everyone, I'd really appreciate some help with this. Thanks!

Comment
Add comment · Show 17
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 rodude123 · May 12, 2016 at 08:37 AM 0
Share

what's the error you get in the inspector

avatar image S_Byrnes rodude123 · May 12, 2016 at 09:10 AM 1
Share

There's no error message

avatar image Odyssee · May 12, 2016 at 08:48 AM 1
Share

If i'm not wrong

About scene restart

Scene numbers are the order they are listed on the build screen

If you start your game on sceneNumber(0), and you ask to load scene 0, then you reload the level you was already in

Have you looked at the build screen and the scene listed in it? Or tried to do loadScene(1) or (2); ?

You can also load a scene by a string loadScene("levelOne"), maybe an alternative. You have sceneFileName.unity, just copy without the .unity

 UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene( "sceneFileName" );

or

 using UnityEngine.Scene$$anonymous$$anagement;
 Scene$$anonymous$$anager.LoadScene( "sceneFileName" );

About no time to see

  if (!half$$anonymous$$oving && Outer)
  {
      ...
      >>>>>>>>>>>>>>Ended = true;
  }
 
  if (!half$$anonymous$$oving && $$anonymous$$iddle)
  {
      ...
      >>>>>>>>>>>>>>Ended = true;
  }
 
  if (!half$$anonymous$$oving && Center)
  {
      ...
      >>>>>>>>>>>>>>Ended = true;
  }
 
  >>>>>>>>>>>>>>if (Ended == true)
  {
      Scene$$anonymous$$anager.LoadScene(sceneNumber, LoadScene$$anonymous$$ode.Single);
  }


Look, in the same update Frame, once it do something, Ended = True and then if Ended = true so change level...

You may use if else (to change level on next frame, in 0.0001 second)

Or may simply but a hidden button somewhere, button that when onClick it will change scene. And so Ended = true will only display button to change scene. (the next one is pseudo code.)

 If(Ended = true){
     display( endGameButton );
 }
avatar image S_Byrnes Odyssee · May 12, 2016 at 09:18 AM 0
Share

Thanks for the response!

How do those loadScene("LevelOne") scripts look? Because I've tried another form like the Application.loadLevel etc but apparently that's no-longer a supported way of doing it.

avatar image Odyssee S_Byrnes · May 12, 2016 at 09:39 AM 1
Share

For me you was doing it good. You have sceneFileName.unity, just copy without the .unity

 UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene( "sceneFileName" );

or

 using UnityEngine.Scene$$anonymous$$anagement;
 Scene$$anonymous$$anager.LoadScene( "sceneFileName" );
Show more comments
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tyche10 · May 12, 2016 at 08:43 AM

Are you sure sceneNumber has the value you want before you try to load the next scene? Maybe check with a Debug.Log before you try to load it.

  if (Ended == true)
              {
     Debug.Log("Next scene: " + sceneNumber);
     
                  SceneManager.LoadScene(sceneNumber, LoadSceneMode.Single);
              }

Did you add the scene in File -> Build Settings?

You can add a delay, to prospone loading the scene until you did the things you wanted to do. Otherwise it will load instantly. You could use for example: http://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html

Comment
Add comment · Show 7 · 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 S_Byrnes · May 12, 2016 at 09:15 AM 0
Share

Thanks for the response! Ok I just tried the debug log and it's looking fine, it comes up with the correct number in each scene, I have 3 scenes by the way, the last one I have set to 0 so it goes back to the first level, but even on the other levels, it shows the correct number (1 and 2 respectively), but it still refreshes the current level.

About the Invoke function, do you need an IEnumerator for that or? Also why is the Example below the code it's Invoking? Isn't that backwards?

avatar image Tyche10 S_Byrnes · May 12, 2016 at 09:27 AM 2
Share

You don't need to use IEnumerator as it is not a coroutine. You can find a decent tutorial about it in the unity tutorial section: http://unity3d.com/learn/tutorials/modules/beginner/scripting/invoke

You could make a function like

 private void LoadNextScene()
 {
       Scene$$anonymous$$anager.LoadScene(sceneNumber, LoadScene$$anonymous$$ode.Single);
 }

and then write in your update

 if (Ended == true)
               {
                      Invoke("LoadNextScene", 3f);
               }

(in this example unity will wait three seconds before executing the LoadNextScene function)

avatar image S_Byrnes Tyche10 · May 12, 2016 at 09:31 AM 0
Share

That's really awesome! Just before your response I had tried the Invoke just like the example anyway and it worked perfectly, two seconds was plenty for me to see the score load up before it refreshed. One second would have sufficed actually.

That Invoke feature is a great thing to know, I've needed something simple like that for ages and IEnumerator never worked for me and was such a hassle, thanks!

The issue of the reloading the same scene still remains though unfortunately.

Show more comments

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

145 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get current scene number 3 Answers

Save/Load Player System 0 Answers

check bool across a script 1 Answer

I'd like my object to be active only when space key is NOT pressed 1 Answer

Not all paths return a value 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