- Home /
multi scripts 1 action the same in each. C#
OK i'll try to explain this the best i can. I have have a main menu that has : New Game, Load Game, Options, and Exit. The Main Menu, New Game, Load Game, and Option are cameras with their own scripts for their GUI's. They all use these var to reference each other.
//Back Button Varabels
public GameObject MMCamra;
public GameObject OPCamra;
public GameObject OPCamra2;
public GameObject OPCamra3;
Ops, New, and Load; all have a back button that does this.
Debug.Log("Switching to Main Menu");
MMCamra.gameObject.SetActive(true);
OPCamra.gameObject.SetActive(false);
OPCamra2.gameObject.SetActive(false);
OPCamra3.gameObject.SetActive(false);
So instead of using these lines of code on each script. i'm trying use one script on empty gameobject to do the above code. I hope i'm making scene here.
Answer by iHaveReturnd · Feb 19, 2014 at 03:26 AM
You could just put that set of code into a function like:
public void GoBack(){
Debug.Log("Switching to Main Menu");
MMCamra.gameObject.SetActive(true);
OPCamra.gameObject.SetActive(false);
OPCamra2.gameObject.SetActive(false);
OPCamra3.gameObject.SetActive(false);
}
And call that function whenever any of those back buttons is hit. I hope I understood you question right but let me know if thats not what you meant
I'm tried that and got this massage.
Assets/Scripts/$$anonymous$$enuScripts/NewGame.cs(175,25): error CS0103: The name `GoBack' does not exist in the current context
How are you calling the function? And are you trying to call it from another script?
If NewGame.cs is a separate script, you'll have to reference the script it's in. So for example (if its in main menu):
public $$anonymous$$ain$$anonymous$$enu main$$anonymous$$enuScript; //declare a var to drag the script onto
//insert conditions for when GoBack should be called, such as button down etc main$$anonymous$$enuScript.GoBack();
Yes im trying to call a function from another script which is on an empty gameObject. The gameObject is called $$anonymous$$anager and has the Button$$anonymous$$anager script on it. Which set which camera is active. im using,
public gameObject $$anonymous$$anage;
To reference the $$anonymous$$anager.
using UnityEngine;
using System.Collections;
public class Button$$anonymous$$anagerScript: $$anonymous$$onoBehaviour {
//Button Var
public GameObject $$anonymous$$$$anonymous$$Camra;
public GameObject OPCamra;
public GameObject OPCamra2;
public GameObject OPCamra3;
public void GoBack()
{
Debug.Log("Switching to $$anonymous$$ain $$anonymous$$enu");
$$anonymous$$$$anonymous$$Camra.gameObject.SetActive(true);
OPCamra.gameObject.SetActive(false);
OPCamra2.gameObject.SetActive(false);
OPCamra3.gameObject.SetActive(false);
}
public void GoNewGame()
{
Debug.Log("Switching to New Game");
$$anonymous$$$$anonymous$$Camra.gameObject.SetActive(false);
OPCamra.gameObject.SetActive(false);
OPCamra2.gameObject.SetActive(true);
OPCamra3.gameObject.SetActive(false);
}
public void GoLoadGame()
{
Debug.Log("Switching to Load Game");
$$anonymous$$$$anonymous$$Camra.gameObject.SetActive(false);
OPCamra.gameObject.SetActive(false);
OPCamra2.gameObject.SetActive(false);
OPCamra3.gameObject.SetActive(true);
}
public void GoOptions()
{
Debug.Log("Switching to Options");
$$anonymous$$$$anonymous$$Camra.gameObject.SetActive(false);
OPCamra.gameObject.SetActive(true);
OPCamra2.gameObject.SetActive(false);
OPCamra3.gameObject.SetActive(false);
}
}
Here is the button im useing.
if (GUI.Button(new Rect(Screen.width * .01f, Screen.height * .9f, Screen.width * .1f, 25),new GUIContent( "Back" , "Back\n\n Go back to the $$anonymous$$ain $$anonymous$$enu")))
{
GoBack();
}
You need to get a reference to your Button$$anonymous$$anagerScript for the button.
Right now it would be trying to call its own "GoBack" function, you can do something like this in the button's script:
public Button$$anonymous$$anagerScript button$$anonymous$$anager; //drag the manager's script in the inspector here
void Update(){
if (GUI.Button(new Rect(Screen.width * .01f, Screen.height * .9f, Screen.width * .1f, 25),new GUIContent( "Back" , "Back\n\n Go back to the $$anonymous$$ain $$anonymous$$enu")))
{
button$$anonymous$$anager.GoBack();
}
}
And that should do it. (If I'm reading the issue correctly)
Yes that was it. Thank you vary much. i can't believe i was having so much trouble with something so simple. thank you again.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134003im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make a menu like ironpants 0 Answers
c# how to get to idle from walk 0 Answers
Alarm lights not changing 1 Answer
What is way to flash the screen WITHOUT using a Texture? 2 Answers