Hello Everybody and thank you to have opened this question
Here’s my Situation:
I have a Options Setup composed of 6 Buttons [Music(On/Off)] [Sound FX(On/Off)] [Windowed(On/Off)], everyone of them has a script called Material_Switch and Material_Switch_Off
here is Material_Switch:
var Mouse_Over_On : Material;
var Mouse_Exit_On : Material;
var TargetOFF : String;
function OnLevelWasLoaded()
{
if( GameObject.Find("Master_Scripts").GetComponent(Master_Options).Music_Status == false)
{
ToOFF();
}
if( GameObject.Find("Master_Scripts").GetComponent(Master_Options).FX_Status == false)
{
ToOFF();
}
}
function OnMouseOver()
{
renderer.material = Mouse_Over_On;
}
function OnMouseExit()
{
renderer.material = Mouse_Exit_On;
}
function OnMouseDown()
{
ToOFF();
}
function ToOFF()
{
GetComponent(MeshRenderer).enabled = false;
GetComponent(MeshCollider).enabled = false;
GetComponent(Material_Switch).enabled = false;
GameObject.Find(TargetOFF).GetComponent(Material_Switch_Off).enabled = true;
GameObject.Find(TargetOFF).GetComponent(MeshRenderer).enabled = true;
GameObject.Find(TargetOFF).GetComponent(MeshCollider).enabled = true;
}
and here is Material_Switch_Off
var Mouse_Over_Off : Material;
var Mouse_Exit_Off : Material;
var TargetON : String;
function OnLevelWasLoaded()
{
if( GameObject.Find("Master_Scripts").GetComponent(Master_Options).Music_Status == true)
{
ToON();
}
if( GameObject.Find("Master_Scripts").GetComponent(Master_Options).FX_Status == true)
{
ToON();
}
}
function OnMouseOver()
{
renderer.material = Mouse_Over_Off;
}
function OnMouseExit()
{
renderer.material = Mouse_Exit_Off;
}
function OnMouseDown()
{
ToON();
}
function ToON()
{
GetComponent(MeshRenderer).enabled = false;
GetComponent(MeshCollider).enabled = false;
GetComponent(Material_Switch_Off).enabled = false;
GameObject.Find(TargetON).GetComponent(Material_Switch).enabled = true;
GameObject.Find(TargetON).GetComponent(MeshRenderer).enabled = true;
GameObject.Find(TargetON).GetComponent(MeshCollider).enabled = true;
}
Everyone of these GameObjects is connected to Master_Scripts, which has the script Material_Options in it, that regulates every Option in the game by calling functions and checking for statuses
here is Master_Options
var Music_Status = true;
var FX_Status = true;
function OnLevelWasLoaded()
{
InitialChecks();
}
function InitialChecks()
{
if(Music_Status == true)
{
MusicON();
}
if(Music_Status == false)
{
MusicOFF();
}
if(FX_Status == true)
{
FXON();
}
if(FX_Status == false)
{
FXOFF();
}
}
function Awake ()
{
DontDestroyOnLoad(this);
}
function MusicON ()
{
GameObject.Find("Background_Music").GetComponent(AudioSource).audio.volume = 0.5;
Music_Status = true;
}
function MusicOFF ()
{
GameObject.Find("Background_Music").GetComponent(AudioSource).audio.volume = 0;
Music_Status = false;
}
function FXON()
{
GameObject.Find("Continue").GetComponent(AudioSource).audio.volume = 0.2;
GameObject.Find("Exit").GetComponent(AudioSource).audio.volume = 0.2;
GameObject.Find("New Game").GetComponent(AudioSource).audio.volume = 0.2;
GameObject.Find("Options").GetComponent(AudioSource).audio.volume = 0.2;
FX_Status = true;
}
function FXOFF()
{
GameObject.Find("Continue").GetComponent(AudioSource).audio.volume = 0;
GameObject.Find("Exit").GetComponent(AudioSource).audio.volume = 0;
GameObject.Find("New Game").GetComponent(AudioSource).audio.volume = 0;
GameObject.Find("Options").GetComponent(AudioSource).audio.volume = 0;
FX_Status = false;
}
function FullScreen ()
{
Screen.fullScreen = true;
}
function Windowed ()
{
Screen.fullScreen = false;
}
The Problem that I’m facing right now is that I want to keep data from Master_Options through Scenes to be able to carry the Mute of the Sound Effects and the Background Music
Master_Scripts is spawned in the Splash Logo (of our Studio) scene, a scene that I use to spawn and create all the Awake and Start function to be then loaded in the game.
It all works fine, but there is a problem with the way Material_Switch works, I first designed it in a way to be applicable to every button in the Options menu, but now I have the problem that I’m calling a function (ToOFF / ToON) checking for the status of Music and SoundFX
This basically means that at the Start the Buttons are all on ON, but if Music_Status or FX_Status are on False, the code will change the buttons to the OFF Object
And I’m sure you already figured out the problem by now, everyone of the six objects has the same scripts (Material_Switch(_Off)) and of course, if Music_Status is on False, it will call the function ToOFF but EVERYONE of the three active objects [Music(On)] [FX(On)] [Windowed(On)] calls the same function, therefore if the Music is off, everything will be Off.
Is there a way I can do this without having to create different script for every GameObject involved?
I tried to be as literal as possible, thanks for your help!