Unity changed loadlevel to sceneload now.I cant find how to load the next scene. i checked reference but not specific on code Help!
I cant find how to load the next scene . i checked reference but not specific on code Help
on certain collisions I tried coding the new scene loading.. I was using loadlevel manager and was working but unity changed this and uses scene manager now
on collision my new scene wont load I have tried using the unity reference code but still not working obviously im doing something wrong but theres so little info on this specific topic since unity just changed it and theres no script answers at all. ive looked for days nothing is out there but the older loadlevel script snippets
plase help the new scene I have is "22"
thankyou
Answer by TBruce · Oct 14, 2016 at 07:22 PM
Here are some functions that I regularly use (in C#)
using UnityEngine.SceneManagement; // required for SceneManager
public void RestartGame()
{
SceneManager.LoadScene(0);
}
public void ReloadCurrentScene()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void LoadPreviouScene()
{
if (SceneManager.GetActiveScene().buildIndex > 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
else
{
SceneManager.LoadScene(0);
}
}
public void LoadNextScene()
{
if (SceneManager.GetActiveScene().buildIndex < (sceneCountInBuildSettings -1)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
else
{
ReloadCurrentScene();
}
}
if you need Unity script let me know. Also, very important, all scenes need to be added to the Scenes in Build list in the Build Settings screen for this to work (see image below). See Unity docs here - under Description.
@sid4 See my modified answer above for more information about Scene$$anonymous$$anager.LoadScene
.
Also it is important to note that Scene$$anonymous$$anager.GetActiveScene() does not take any parameters, it returns a Scene$$anonymous$$anagement.Scene structure.
It is very important that all scenes be added to the Scenes in Build list in the Build Settings screen for this to work (see image here). See Unity docs here - under Description.
nope I tried several of your codes but its not working heres one I tried
using System;
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
namespace UnityStandardAssets._2D
{
public class lo : $$anonymous$$onoBehaviour {
//void FixedUpdate() {
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "Player")
//Destroy (gameObject);
// Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetSceneAt(11).name);
{
if (Scene$$anonymous$$anager.GetActiveScene().buildIndex < (sceneCountInBuildSettings -1)
{
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene(22).buildIndex + 1);
}
else
{
ReloadCurrentScene();
}
}
//Application.LoadLevel("11");
}
}
I got the scene to play and your rite I had to add the scenes to the build manually
but the only code that worked is the link u gave me
but it loaded repetitive tons of the scene at the same time on every collision it froze my pc so that additive code wont do it since I guess additive (by nature'} means too keep adding the scene on every collision I just want to add /change to the next scene on collision
I need a code to just change the scene on a collision not add tons of the scenes just change the scene on collision
I have tried all your codes but none work, heres the latest code im with
using System;
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
namespace UnityStandardAssets._2D
{
public class lo : $$anonymous$$onoBehaviour {
//void FixedUpdate() {
//void OnCollisionEnter2D(Collision2D coll) {
void OnCollisionEnter2D(Collision2D coll) {
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
//Scene$$anonymous$$anager.LoadScene("22");
//Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene(22).buildIndex + 1);
//Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene(22).name);
//Scene$$anonymous$$anager.LoadScene(22);
}
}
}
i just tried all the links single and or additive codes the additive did work but - again it keeps adding hundreds of scenes within the scene and freezes my pc that's not what I want at all
I just want a normal transition to the next scene on collision
Why are you trying to load the scenes in Additive
mode? If I understand you, this is your current code
using System; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement;
namespace UnityStandardAssets._2D { public class lo : $$anonymous$$onoBehaviour { void OnCollisionEnter2D(Collision2D coll) { Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1); } } } It would seem that the OnCollisionEnter2D()
is constantly being triggered. I do not know how your scene is set up but the problem can most likely be remedied by doing something like this
using System;
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
namespace UnityStandardAssets._2D
{
public class lo : $$anonymous$$onoBehaviour
{
private bool sceneIsLoading = false;
void OnCollisionEnter2D(Collision2D coll)
{
// do not try to load scene if sceneIsLoading is set
if (!sceneIsLoading)
{
// set here so this block is never entered again
sceneIsLoading = true;
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1);
}
}
}
}
Now you can also do something like this
using System;
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
namespace UnityStandardAssets._2D
{
public class lo : $$anonymous$$onoBehaviour
{
private int sceneIsLoading = -1;
void OnCollisionEnter2D(Collision2D coll)
{
// do not try to load scene if sceneIsLoading is set
if (sceneIsLoading< 0)
{
// set here so this block is never entered again
sceneIsLoading = Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1;
Scene$$anonymous$$anager.LoadScene(sceneIsLoading);
}
}
}
}
But remember this
Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1
is not safe, this is because you will get an error if Scene$$anonymous$$anager.GetActiveScene().buildIndex
is the last scene.
That is where my original script comes in. You create a class (call it Game$$anonymous$$anager if you will), add the code that I originally provided.
Where ever it is needed do something like
public class lo : $$anonymous$$onoBehaviour
{
public Game$$anonymous$$anager game$$anonymous$$anager;
private bool sceneIsLoading = false;
void OnCollisionEnter2D(Collision2D coll)
{
// do not try to load scene if sceneIsLoading is set
if (!sceneIsLoading)
{
// set here so this block is never entered again
sceneIsLoading = true;
if (game$$anonymous$$anager != null)
{
game$$anonymous$$anager.LoadNextScene();
}
}
}
}
ok I will try this tomorrow and get back to you
thanks and I will be in touch I hope this works
Your answer
Follow this Question
Related Questions
How do I trigger a game object to move a desired distance at a desired speed? 1 Answer
How to change button.spriteState.pressedSprite sprite by script - Unity 4.7.1 1 Answer
Using multiple materials on a 2d mesh 0 Answers
Problem with c# script 0 Answers
ObjectParameter Usage in a Custom Skybox 0 Answers