The question is answered, right answer was accepted
Object looses Inspector properties When Scene returns
OK this issues has been plaguing me for what feels like forever and no one seems to explain right what I need to do to fix the issue. I have a game object that when my scene changes it looses connection to items in the inspector when I return to the scene. I have tried this:
I added a video demo link to try and better explain what I mean. There must be a way of dealing with this issue?
// Use this for initialization
void Start () {
StartCoroutine(OpenConnection());
lightSlider = GetComponent<Slider> ();
if(slider == null) slider = GetComponent<Slider>(); // search slider in this object if its not set in unity inspector
if(source == null) source = FindObjectOfType<VoiceBox>().GetComponent<AudioSource>(); // search audiosource in this object if its not set in unity inspector
}
Tos see if I could get the start function to re - assign the Audio course into the inspector but it does nothing when I return to my main scene.
Here is a link to a demo video of what I mean Video Demo
@hexagonius, Both, if I got to scene 2 and then return to scene one the objects in the inspector are missing, now just to be clear I do not want these items to carry across to scene 2 I just want them placed back when I return to scene 1.
Answer by KnightRiderGuy · Jan 18, 2016 at 05:46 PM
Turns out what I had to do was create an empty game object in my main scene that I call "DataHolder" and this object holds the references to the UI slider, AudioSource and UI text.
Then in my Init object on it's "Sending" Script I do this:
void OnLevelWasLoaded (int level)
{
if (level == 2) {
DataHolder stor = GameObject.Find ("DataHolder").GetComponent<DataHolder> ();
//Variable names here
source = stor.source;
TemperatureText = stor.TemperatureText;
slider = stor.slider;
}
}
That way when I come back to my main scene which is level 2 my "Init" object is repopulated with the references to the UI slider, AudioSource and UI text.
What a pain that was getting that information.
Hi zentaiguy, So you have done a "safestate" of all your variable in an empty GO? I have also some problems of variable which become empty for no reason... Thanks, Clément