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!
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 );
}
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.
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" );
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
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?
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)
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.
Your answer
Follow this Question
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