Can't Restart Scene in unity 3D
In my game I have several levels, each one has 6 scenes, the scenes names are: Scene 1, Scene 2, Scene 3 ...etc...
When the player lose, the current scene should be restarted, so I used this code:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
I didn't use "(...)GetActiveScene().name);" because the scenes names are repeated on each level.
The problem I have is when the player lose in level2 (scene 6), the scene 6 of level1 is loaded instead of restarting scene 6 of level2. Do you know what's the problem in my code?
c# unity3d
add a comment |
In my game I have several levels, each one has 6 scenes, the scenes names are: Scene 1, Scene 2, Scene 3 ...etc...
When the player lose, the current scene should be restarted, so I used this code:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
I didn't use "(...)GetActiveScene().name);" because the scenes names are repeated on each level.
The problem I have is when the player lose in level2 (scene 6), the scene 6 of level1 is loaded instead of restarting scene 6 of level2. Do you know what's the problem in my code?
c# unity3d
Are you sure the buildIndex for the active scene is the number you're expecting? Something could have happened in the configuration to put the indexes out of sequence (at least that's happened to me before)
– jdmac020
Dec 31 '18 at 15:25
add a comment |
In my game I have several levels, each one has 6 scenes, the scenes names are: Scene 1, Scene 2, Scene 3 ...etc...
When the player lose, the current scene should be restarted, so I used this code:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
I didn't use "(...)GetActiveScene().name);" because the scenes names are repeated on each level.
The problem I have is when the player lose in level2 (scene 6), the scene 6 of level1 is loaded instead of restarting scene 6 of level2. Do you know what's the problem in my code?
c# unity3d
In my game I have several levels, each one has 6 scenes, the scenes names are: Scene 1, Scene 2, Scene 3 ...etc...
When the player lose, the current scene should be restarted, so I used this code:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
I didn't use "(...)GetActiveScene().name);" because the scenes names are repeated on each level.
The problem I have is when the player lose in level2 (scene 6), the scene 6 of level1 is loaded instead of restarting scene 6 of level2. Do you know what's the problem in my code?
c# unity3d
c# unity3d
asked Dec 31 '18 at 14:13
TaikTaik
757
757
Are you sure the buildIndex for the active scene is the number you're expecting? Something could have happened in the configuration to put the indexes out of sequence (at least that's happened to me before)
– jdmac020
Dec 31 '18 at 15:25
add a comment |
Are you sure the buildIndex for the active scene is the number you're expecting? Something could have happened in the configuration to put the indexes out of sequence (at least that's happened to me before)
– jdmac020
Dec 31 '18 at 15:25
Are you sure the buildIndex for the active scene is the number you're expecting? Something could have happened in the configuration to put the indexes out of sequence (at least that's happened to me before)
– jdmac020
Dec 31 '18 at 15:25
Are you sure the buildIndex for the active scene is the number you're expecting? Something could have happened in the configuration to put the indexes out of sequence (at least that's happened to me before)
– jdmac020
Dec 31 '18 at 15:25
add a comment |
2 Answers
2
active
oldest
votes
Maybe not an answer to why this happens in your specific case but two suggestions I would prefer:
Just a workaround but how about naming your scenes properly instead like e.g. Level1_Scene1
, Level2_Scene3
etc
so they are all uniquely identifiable using GetActiveScene().name
Alternatively you could also try using GetActiveScene().path
(which is allways unique) since LoadScene
takes
Name or path of the Scene to load.
I never thought about using the path--that's a really great option without having to rename anything.
– jdmac020
Dec 31 '18 at 17:40
1
Using path is the best solution for me, because I can't rename the scenes. Thank you!
– Taik
Dec 31 '18 at 18:01
add a comment |
Using the Scene name to target certain scenes seems like the best way to go. Also, it is worth naming your scenes in a distinguishable way since other people may have trouble understanding your code or you may have trouble using Debug.Log
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988423%2fcant-restart-scene-in-unity-3d%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Maybe not an answer to why this happens in your specific case but two suggestions I would prefer:
Just a workaround but how about naming your scenes properly instead like e.g. Level1_Scene1
, Level2_Scene3
etc
so they are all uniquely identifiable using GetActiveScene().name
Alternatively you could also try using GetActiveScene().path
(which is allways unique) since LoadScene
takes
Name or path of the Scene to load.
I never thought about using the path--that's a really great option without having to rename anything.
– jdmac020
Dec 31 '18 at 17:40
1
Using path is the best solution for me, because I can't rename the scenes. Thank you!
– Taik
Dec 31 '18 at 18:01
add a comment |
Maybe not an answer to why this happens in your specific case but two suggestions I would prefer:
Just a workaround but how about naming your scenes properly instead like e.g. Level1_Scene1
, Level2_Scene3
etc
so they are all uniquely identifiable using GetActiveScene().name
Alternatively you could also try using GetActiveScene().path
(which is allways unique) since LoadScene
takes
Name or path of the Scene to load.
I never thought about using the path--that's a really great option without having to rename anything.
– jdmac020
Dec 31 '18 at 17:40
1
Using path is the best solution for me, because I can't rename the scenes. Thank you!
– Taik
Dec 31 '18 at 18:01
add a comment |
Maybe not an answer to why this happens in your specific case but two suggestions I would prefer:
Just a workaround but how about naming your scenes properly instead like e.g. Level1_Scene1
, Level2_Scene3
etc
so they are all uniquely identifiable using GetActiveScene().name
Alternatively you could also try using GetActiveScene().path
(which is allways unique) since LoadScene
takes
Name or path of the Scene to load.
Maybe not an answer to why this happens in your specific case but two suggestions I would prefer:
Just a workaround but how about naming your scenes properly instead like e.g. Level1_Scene1
, Level2_Scene3
etc
so they are all uniquely identifiable using GetActiveScene().name
Alternatively you could also try using GetActiveScene().path
(which is allways unique) since LoadScene
takes
Name or path of the Scene to load.
edited Dec 31 '18 at 20:57
answered Dec 31 '18 at 17:20
derHugoderHugo
6,17431230
6,17431230
I never thought about using the path--that's a really great option without having to rename anything.
– jdmac020
Dec 31 '18 at 17:40
1
Using path is the best solution for me, because I can't rename the scenes. Thank you!
– Taik
Dec 31 '18 at 18:01
add a comment |
I never thought about using the path--that's a really great option without having to rename anything.
– jdmac020
Dec 31 '18 at 17:40
1
Using path is the best solution for me, because I can't rename the scenes. Thank you!
– Taik
Dec 31 '18 at 18:01
I never thought about using the path--that's a really great option without having to rename anything.
– jdmac020
Dec 31 '18 at 17:40
I never thought about using the path--that's a really great option without having to rename anything.
– jdmac020
Dec 31 '18 at 17:40
1
1
Using path is the best solution for me, because I can't rename the scenes. Thank you!
– Taik
Dec 31 '18 at 18:01
Using path is the best solution for me, because I can't rename the scenes. Thank you!
– Taik
Dec 31 '18 at 18:01
add a comment |
Using the Scene name to target certain scenes seems like the best way to go. Also, it is worth naming your scenes in a distinguishable way since other people may have trouble understanding your code or you may have trouble using Debug.Log
.
add a comment |
Using the Scene name to target certain scenes seems like the best way to go. Also, it is worth naming your scenes in a distinguishable way since other people may have trouble understanding your code or you may have trouble using Debug.Log
.
add a comment |
Using the Scene name to target certain scenes seems like the best way to go. Also, it is worth naming your scenes in a distinguishable way since other people may have trouble understanding your code or you may have trouble using Debug.Log
.
Using the Scene name to target certain scenes seems like the best way to go. Also, it is worth naming your scenes in a distinguishable way since other people may have trouble understanding your code or you may have trouble using Debug.Log
.
answered Dec 31 '18 at 17:29
SpiritSealSpiritSeal
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988423%2fcant-restart-scene-in-unity-3d%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you sure the buildIndex for the active scene is the number you're expecting? Something could have happened in the configuration to put the indexes out of sequence (at least that's happened to me before)
– jdmac020
Dec 31 '18 at 15:25