- Home /
The question is answered, right answer was accepted
Public Scene Variables not appearing in the inspector
I am aware that this question has been asked before but I have searched for the answer and none of the proposed solutions have worked for me. I have checked both of the scripts in my project currently for errors and have found none. My variable is public.
Script the variable is in:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class doorEnterController : MonoBehaviour {
public Scene toLoad;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
The other script in my project:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerController : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.W))
{
gameObject.transform.Translate(new Vector3(0, 0.1f, 0));
}
if (Input.GetKey(KeyCode.S))
{
gameObject.transform.Translate(new Vector3(0, -0.1f, 0));
}
if (Input.GetKey(KeyCode.A))
{
gameObject.transform.Translate(new Vector3(-0.1f, 0, 0));
}
if (Input.GetKey(KeyCode.D))
{
gameObject.transform.Translate(new Vector3(0.1f, 0, 0));
}
}
}
Hi @drearyplane ,
Scene$$anonymous$$anager.LoadScene(); This can load scenes with either the name or build index (Found in Build Settings). ~Check overloads for extra functionality~ e.g. Scene$$anonymous$$anager.LoadScene(1); Scene$$anonymous$$anager.LoadScene("Scene2");
So, you could make a script like this, with the name, or index, exposed to the editor, you can put the name, or index, in the field in inspector that you would like to load.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
public class SceneChanger : $$anonymous$$onoBehaviour
{
[SerializeField] private int SceneIndex;
[SerializeField] private string SceneName;
public void ChangeScene()
{
List<Scene> allScenes = new List<Scene>();
for (int i = 0; i < Scene$$anonymous$$anager.sceneCount; i++)
{
allScenes.Add(Scene$$anonymous$$anager.GetSceneAt(i));
}
//Use one of these options vvvvvvvvvvvvvvvvvvvvvvvvv
// 1. Use this if you're using the Scene Name. (CASE SENSITIV$$anonymous$$.. I think)
foreach (Scene scene in allScenes)
{
if(scene.name == SceneName)
{
Scene$$anonymous$$anager.LoadScene(SceneName);
}
}
// 2. Can also use the scene index (probably best practice)
foreach (Scene scene in allScenes)
{
if (scene.buildIndex == SceneIndex)
{
Scene$$anonymous$$anager.LoadScene(SceneName);
}
}
}
}
Answer by eses · Sep 01, 2018 at 11:49 AM
Hi @drearyplane
Unity editor doesn't serialize that many types, and Scene is not derived from those that are serializable... (UnityEngine.Object, GameObject, ScriptableObject, primitive data types etc).
So having a public in front of field doesn't automatically make something serializable in Unity inspector, if at all. If you are expecting similar behavior like you'd get with TextAsset (being able to drag'n'drop text file to inspector field) you are not going to get it I guess...
Someone more skilled can probably give you more technical answer, but hope this helps.
See:
https://docs.unity3d.com/Manual/script-Serialization.html
https://docs.unity3d.com/ScriptReference/SceneManagement.Scene.html
Thanks for the help, I've just decided to store a string to hold the scene name, it was nothing too critical. I'm working in a game jam so the quick answer was appreciated, thank you
Answer by Ermiq · Sep 01, 2018 at 11:49 AM
Look here: https://forum.unity.com/threads/how-to-link-scenes-in-the-inspector.383140/ - a custom script for the editor to be able to reference scenes in the inspector;
and here: https://docs.unity3d.com/ScriptReference/SceneAsset.html - official Unity documentation about referencing scenes in the Editor.
So, you can't just make a scene variable visible in the Inspector as you do with other variables.
Thanks for the help, I've just decided to store a string to hold the scene name, it was nothing too critical. I'm working in a game jam so the quick answer was appreciated, thank you - yes that was the same as what I commented on the other guy, not much else to say