How to do this?

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!

I am not coding in java but I liked the effort you put here to explain so I give it a shot.
Firstly if you use the same script on several buttons it should have some sort of index to determine which button you click.I fused your material on and off scripts to one. I move the initial button update to Master script. The idea is you have index on each button which you set in the inspector, bool state (true for On button and false for off) and reference to gameobject of the opposite button (audioOFf for audioOn etc). On click you call method in masterscript passing those two parameters so you will know which function to call.

This is my first work in Java, you should switch to c# really , java sux.

Here are the scripts (I am not sure if they work, but at least show you idea).

here the scripts:
matSwitch.js put it on every button and setup parameters in the inspector.

var Mouse_Over_On : Material;
var Mouse_Exit_On : Material;
var TargetOpposite : GameObject; // drag here the opposite button 
var Index : int; // 0 for audio 1 for sound 2 for fullscreen button
var ButtonOn : boolean; // set true for all buttons of ON type
  
function OnMouseOver()
    {
        renderer.material = Mouse_Over_On;
    }
 
function OnMouseExit()
    {
        renderer.material = Mouse_Exit_On;
    }
 
function OnMouseDown()
    {
    	//update the button 
        ToONOFF();
        //i have no idea how to do it in java but idea is to call the CallMEthods function in your 
        //master scripts. So it will actually do what your button is meant to you pass index and type to know which function to call 
        var Master : Master_Options;
        Master = GameObject.Find("Master_Scripts").GetComponent(Master_Options);
        Master.CallMethods(Index, ButtonOn);
    }
 
function ToONOFF() // it will disable the button and enable oppposite 
    {
        GetComponent(MeshRenderer).enabled = false;
        GetComponent(MeshCollider).enabled = false;
        GetComponent(matSwitch).enabled = false;
        TargetOpposite.GetComponent(matSwitch).enabled = true;
        TargetOpposite.GetComponent(MeshRenderer).enabled = true;
        TargetOpposite.GetComponent(MeshCollider).enabled = true;
    }

your Master_Options.js shortened a bit,

var Music_Status = true;
var FX_Status = true;
 

var MusixOnGO : GameObject; //drag here your buttons in inspector
var MusixOFFGO : GameObject; 
var AudioOnGO : GameObject; 
var AudioOFFGO : GameObject; 

function OnLevelWasLoaded()
    {
        InitialChecks();
    }
 
function InitialChecks()
    {
       var script : matSwitch; 
         if(Music_Status)
           { 
           		MusicON(true);
           		//Initial update button
           		script = MusixOnGO.GetComponent(matSwitch);
           		script.ToONOFF();
           }
      	   else
           { 
           		MusicON(false);
           		//Initial update button
  			    script = MusixOFFGO.GetComponent(matSwitch);
           		script.ToONOFF();
           }
            
        
        if(FX_Status == true)
        	{
        		FXON(true);
        		//Initial update button
        		script = AudioOnGO.GetComponent(matSwitch);
           		script.ToONOFF();
        	}
        else
        	{
        		FXON(false);
        		//Initial update button
        		script = AudioOFFGO.GetComponent(matSwitch);
           		script.ToONOFF();
      		}
       		
    }
 
function Awake () 
    {
        DontDestroyOnLoad(this);
    }

function MusicON ( status:boolean ) // true for ON and false for OFF
    {
   		 var volume : double;
   		if(status) 
        {
	       volume = 0.5;
        }
        else
        {
	       volume = 0;    
	       
  		}
  		 GameObject.Find("Background_Music").GetComponent(AudioSource).audio.volume = 0.5;
	        Music_Status = status;
    }
 

function FXON(status:boolean) // true for ON and false for OFF
{
 var volume : double;
    if(status)
    {
        volume = 0.2;
    }
    else
    {
    	volume = 0;
       
    }
        GameObject.Find("Continue").GetComponent(AudioSource).audio.volume = volume;
        GameObject.Find("Exit").GetComponent(AudioSource).audio.volume = volume;
        GameObject.Find("New Game").GetComponent(AudioSource).audio.volume = volume;
        GameObject.Find("Options").GetComponent(AudioSource).audio.volume = volume;
     FX_Status = status;
 }

 
function FullScreen (status:boolean) // true for ON and false for OFF
    {
        Screen.fullScreen = status;
    }
    
    //here we call methods basing on which button was clicked 0 for audio 1 for sound 2 for fullscreen button
    //true for on and false for off button
function CallMethods(index: int , status : boolean) // call method
{
 if(index == 0) //index zero so we modify status of audio 
 FXON(status); //passing the status direclty so if true we pass true. 
 if(index == 1) //analoguosly..
 MusicON(status);
 if(index == 2) // index 2 call fullscreen
 FullScreen(status);

} 

Could be probably done many others ways I don’t have setup to test it and as I said, I hate Java :slight_smile:

smallbit I must thank you, altho I didn’t use your way of solving this problem, but grasped the concept, I made these:

Master_Options

var Music_Status : boolean = true;
var FX_Status : boolean = true;
var Windowed_Status : boolean = true;

function OnLevelWasLoaded()
	{
		InitialChecks();
	}
	
function InitialChecks( )
	{
		if(Music_Status == true)
			{
				MusicON();
			}
		else
			{
				MusicOFF();
			}	
		if(FX_Status == true)
			{
				FXON();
			}
		else
			{
				FXOFF();
			}
	}
	
function Awake () 
	{
		DontDestroyOnLoad(this);
	}
	
function CheckIndex ()
	{
		var Music_Button : GameObject = GameObject.Find("Options_Music");
		var Fx_Button : GameObject = GameObject.Find("Options_Fx"); 
		var Windowed_Button : GameObject = GameObject.Find("Options_Screen_Windowed");
			if ( Music_Button.Index == 1 )
			{
				if (Music_Status == true)
				{
					MusicOFF();
					Music_Status = false;
				}
			else
				{
					MusicON();
					Music_Status = true;
				}
			}
				if ( Fx_Button.Index == 2 )
			{
				if ( FX_Status == true )
				{
					FXOFF();
					FX_Status = false;
				}
			else
				{
					FXON();
					FX_Status = true;
				}
			}
	}	

function MusicOFF ()
	{
		var Music_Button : GameObject = GameObject.Find("Options_Music");
		var Music_Button_Off : GameObject = GameObject.Find("Options_Music_Off");
		Music_Button.GetComponent(MeshRenderer).enabled = false;
		Music_Button.GetComponent(MeshCollider).enabled = false;
		Music_Button.GetComponent(Material_MSC).enabled = false;
		Music_Button_Off.GetComponent(Material_MSC_Off).enabled = true;
		Music_Button_Off.GetComponent(MeshRenderer).enabled = true;
		Music_Button_Off.GetComponent(MeshCollider).enabled = true;
		GameObject.Find("Background_Music").GetComponent(AudioSource).audio.volume = 0;
		Music_Status = false;
	}

function MusicON ()
	{
		var Music_Button : GameObject = GameObject.Find("Options_Music");
		var Music_Button_Off : GameObject = GameObject.Find("Options_Music_Off");
		Music_Button.GetComponent(MeshRenderer).enabled = true;
		Music_Button.GetComponent(MeshCollider).enabled = true;
		Music_Button.GetComponent(Material_MSC).enabled = true;
		Music_Button_Off.GetComponent(Material_MSC_Off).enabled = false;
		Music_Button_Off.GetComponent(MeshRenderer).enabled = false;
		Music_Button_Off.GetComponent(MeshCollider).enabled = false;
		GameObject.Find("Background_Music").GetComponent(AudioSource).audio.volume = 0.5;	
		Music_Status = true;
	}
	
function FXON()
	{
		var Fx_Button : GameObject = GameObject.Find("Options_Fx");
		var Fx_Button_Off : GameObject = GameObject.Find("Options_Fx_Off");
		Fx_Button.GetComponent(MeshRenderer).enabled = true;
		Fx_Button.GetComponent(MeshCollider).enabled = true;
		Fx_Button.GetComponent(Material_MSC).enabled = true;
		Fx_Button_Off.GetComponent(Material_MSC_Off).enabled = false;
		Fx_Button_Off.GetComponent(MeshRenderer).enabled = false;
		Fx_Button_Off.GetComponent(MeshCollider).enabled = false;
		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()
	{
		var Fx_Button : GameObject = GameObject.Find("Options_Fx");
		var Fx_Button_Off : GameObject = GameObject.Find("Options_Fx_Off");
		Fx_Button.GetComponent(MeshRenderer).enabled = false;
		Fx_Button.GetComponent(MeshCollider).enabled = false;
		Fx_Button.GetComponent(Material_MSC).enabled = false;
		Fx_Button_Off.GetComponent(Material_MSC_Off).enabled = true;
		Fx_Button_Off.GetComponent(MeshRenderer).enabled = true;
		Fx_Button_Off.GetComponent(MeshCollider).enabled = true;
		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;
		Windowed_Status = false;
	}

function Windowed ()
	{
		Screen.fullScreen = false;
		Windowed_Status = true;
	}

Material_MSC

var Mouse_Over_On : Material;
var Mouse_Exit_On : Material;
var TargetOFF : String;

var Master_Script : GameObject;
var Index : int;

function OnLevelWasLoaded()
	{
		if( GameObject.Find("Master_Scripts").GetComponent(Master_Options).Music_Status == false)
			{
				Master_Script.GetComponent(Master_Options).MusicOFF();
			}
		if( GameObject.Find("Master_Scripts").GetComponent(Master_Options).Fx_Status == false)
			{
				Master_Script.GetComponent(Master_Options).FXOFF();
			}
	}

function OnMouseOver()
	{
		renderer.material = Mouse_Over_On;
	}
	
function OnMouseExit()
	{
		renderer.material = Mouse_Exit_On;
	}
	
function OnMouseDown()
	{
		Master_Script.GetComponent(Master_Options).CheckIndex();
	}

Material_MSC_Off

var Mouse_Over_On : Material;
var Mouse_Exit_On : Material;
var TargetOFF : String;

var Master_Script : GameObject;
var Index : int;

function OnLevelWasLoaded()
	{
		if( GameObject.Find("Master_Scripts").GetComponent(Master_Options).Music_Status == true)
			{
				Master_Script.GetComponent(Master_Options).MusicON();
			}
		if( GameObject.Find("Master_Scripts").GetComponent(Master_Options).Fx_Status == true)
			{
				Master_Script.GetComponent(Master_Options).FXON();
			}
	}

function OnMouseOver()
	{
		renderer.material = Mouse_Over_On;
	}
	
function OnMouseExit()
	{
		renderer.material = Mouse_Exit_On;
	}
	
function OnMouseDown()
	{
		Master_Script.GetComponent(Master_Options).CheckIndex();
	}

Thanks to smallbit I came to learn how Index variables can help you use the same script on different gameobjects and still thanks to him I managed to polish the code with booleans and to expand my Master_options to have ven more control on the settings

One thing that is important is that I still don’t have MSC and MSC_Off as one just for brute debugging, in case something goes wrong I have all my On and OFF script layed out

hope this helps somebody else!