How do i create a scene variable?
I cant seem to find the right answer for this. I want a variable which contains scenes and you can access from the editor. I want to be able to drag a scene from assets into it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChanger : MonoBehaviour {
public Scene level;
void OnTriggerEnter2D(Collider2D coll) {
if(coll.gameObject.name == "Player") {
//Here it would use the "level" variable to load the next scene
}
}
}
As far as I know, you can't make a variable out of a scene.
$$anonymous$$y strategy for this, though, is to take the entire hierarchy of my scene, and child it under an empty game object.
I then make this a prefab. In my case, I use this to make setting up a new level easier, but I see no reason why you couldn't have one for each level and store a collection of these in a script if you wanted.
You can't. The editor can only edit scenes, not references to scenes. You have to refer to scenes in code by name (a string) or build number (an integer).
Answer by PierreSydo · Jun 23, 2017 at 04:17 AM
I'm not sure that you can store an entire scene, but you can store the name of the scene as a string and launch it using the SceneManager.LoadScene method
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChanger : MonoBehaviour {
public string levelName;
void OnTriggerEnter2D(Collider2D coll) {
if(coll.gameObject.name == "Player") {
// Loading the scene from it's name
SceneManager.LoadScene(levelName);
}
}
}
Answer by Linkthehylian04 · Jun 21, 2017 at 04:30 PM
Try this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChanger : MonoBehaviour {
void OnTriggerEnter2D(Collider2D coll) {
if(coll.gameObject.name == "Player") {
SceneManager.LoadScene (sceneName:"level");
}
}
}
Instead of using a scene variable, you can use the scene's name to switch between scenes.
The only thing is, i want to use this prefab for multiple places and different scenes. The variable was there so that i could easily edit which scene it sends the player too. However after some more research it seems as though this is impossible, I can find one example where someone has done this. But thank you anyway
You dont need a reference to the scene prefab, just call it by name as I did in my comment. If you need to change what scene the player is sent to, just change the variable.
Answer by · Jun 21, 2017 at 04:41 PM
Just pass the Scene name and call it. Remember to add the scene in the build settings.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChanger : MonoBehaviour {
public String level;
void OnTriggerEnter2D(Collider2D coll) {
if(coll.gameObject.name == "Player") {
//Here it would use the "level" variable to load the next scene
SceneManager.LoadScene (level);
}
}
}
This doesn't work. You need sceneName:"" or sceneBuildIndex:"" inside of the paretheses for it to work.
No, you don't. Just pass the scene name as String. https://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene.html