- Home /
Object not surviving on load?
So here's the deal, I have a MenuOptions object with a GUI.Toggle button currently only toggling music on and off. What works is that it toggles the music on and off in the menu, but as soon as I press New Game or Continue and load another scene the toggle button is gone. And I've set the DontDestroyOnLoad(gameObject); in the MenuOptions script
What this script does is just has a toggle button, and accesses the MenuDJ and DJ objects and scripts which play music to control their volume. The Menu part works, but when it moves to the next scene that object doesn't exist anymore.
Also im getting errors cause it can't find DJ in the menu scene so id like it first to check if theres an object with that tag there before going on further. And for the menu object not to get destroyed when crossing to the next scene ofc. Sorry for such a messy question I'm kind of in a rush so I wanted to post the Q before I leave.
Here's the code:
#pragma strict
DontDestroyOnLoad (transform.gameObject);
var muteToggle = false;
var menuDJscript : MenuDJ;
var djScript : DJ;
var menuDJ : GameObject;
var dj : GameObject;
function Start()
{
menuDJscript = menuDJ.gameObject.FindWithTag("MenuDJ").GetComponent(MenuDJ);
djScript = dj.gameObject.FindWithTag("DJ").GetComponent(DJ);
}
function OnGUI ()
{
muteToggle = GUI.Toggle (Rect (25, 25, 100, 30), muteToggle, "Mute Music");
if(muteToggle == false)
{
menuDJscript.audio.volume = 1.0;
djScript.audio.volume = 1.0;
Debug.Log ("Music On");
}
if(muteToggle == true)
{
menuDJscript.audio.volume = 0.0;
djScript.audio.volume = 0.0;
Debug.Log ("Music Off");
}
}
put the DontDestroy() stuff in Awake() ...
Awake() { DontDestroy(); }
Tried again but now it does work except it doesn't turn the music on or off after i change the scene.
I'm assu$$anonymous$$g is cause it doesn't look for the DJ tag again since it did only the first time in the main menu?
Is there a function that starts every time a level loads?
Answer by ChrisSch · Jul 28, 2013 at 07:23 PM
I rewrote part of it and now it works. Thank you for your time Lo0NuhtiK. :)
#pragma strict
var muteToggle = false;
var menuDJscript : MenuDJ;
var djScript : DJ;
var menuDJ : GameObject;
var dj : GameObject;
function Awake()
{
DontDestroyOnLoad (gameObject);
}
function OnGUI ()
{
muteToggle = GUI.Toggle (Rect (25, 25, 100, 30), muteToggle, "Mute Music");
if(muteToggle == false)
{
if (GameObject.FindWithTag("MenuDJ") != null)
{
menuDJscript = menuDJ.gameObject.FindWithTag("MenuDJ").GetComponent(MenuDJ);
menuDJscript.audio.volume = 1.0;
}
if (GameObject.FindWithTag("DJ") != null)
{
djScript = dj.gameObject.FindWithTag("DJ").GetComponent(DJ);
djScript.audio.volume = 1.0;
}
}
if(muteToggle == true)
{
if (GameObject.FindWithTag("MenuDJ") != null)
{
menuDJscript = menuDJ.gameObject.FindWithTag("MenuDJ").GetComponent(MenuDJ);
menuDJscript.audio.volume = 0.0;
}
if (GameObject.FindWithTag("DJ") != null)
{
djScript = dj.gameObject.FindWithTag("DJ").GetComponent(DJ);
djScript.audio.volume = 0.0;
}
}
}