# Calling and functions/methods from different Classes in Unity

I am learning some of the basics with Unity and am running into calling/sending code to a different script.

For example, the code below is a basic menu of options. I have a different Scene set up in Unity for each one of the menu options. As you can see from the if/else if statements, depending on which option a user checks, will take them to a different portion of the program.

At this point it will obviously not compile because the 4 methods for each menu option are not referenced anywhere.

Lets say the user selects option 1 which is the StudyScreen() method. Now in the StudyScreen.cs script that I created, how do I link those so when option 1 is selected, it goes to the StudyScreen.cs and runs what I need it to run.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class MainMenu : MonoBehaviour {

// Use this for initialization
void Start () {
"1. Study\r\n" +
"2. Quiz\r\n" +
"3. Test\r\n" +
"4. Exit\r\n";
}

// Update is called once per frame
void Update () {
if      (Input.GetKeyDown(KeyCode.Alpha1))  {StudyScreen();}
else if (Input.GetKeyDown(KeyCode.Alpha2))  {QuizScreen();}
else if (Input.GetKeyDown(KeyCode.Alpha3))  {TestScreen();}
else if (Input.GetKeyDown(KeyCode.Alpha4))  {ExitScreen();}

}
}


I hope I was able to make it clear as to what I was trying to accomplish. Please let me know if anything is unclear.

• Something like this? gamedev.stackexchange.com/questions/93372/…
– House
Jul 23 '15 at 16:32
• @Byte56 I wouldn't use that link - it is the old UI system in Unity. Jul 25 '15 at 21:50

It depends on whether or not you have put your script on a game object. (Monobehavior is for that purpose.) If you have not, create an instance of it the normal way.

StudyScreen studyScreen = new StudyScreen();


void Update ()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
studyScreen.StudyScreen();
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
studyScreen.QuizScreen();
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
studyScreen.TestScreen();
}
else if (Input.GetKeyDown(KeyCode.Alpha4))
{
studyScreen.ExitScreen();
}
}


If you have, then you can simply find the instance of that script component by utilizing the GetComponent method and assign it to the variable. Your code would be:

StudyScreen studyScreen;

void Start()
{
studyScreen = GetComponent<StudyScreen>();
}


The update method would be the same as above.

Now, that being said you should consider using their UI mechanics instead. You can add click events through the Unity UI instead of coding it up. Take a look at the tutorials that they've done:

If you do not yet have a grasp on how components in Unity work, I highly suggest you read on it. It is fundamental to creating games in Unity.

Basicaly, attach both scripts to the same gameobject (to make it simple) and in the MainMenu script just add the following variable

StudyScreen sc;


then in the Start() initialize it as follows

sc = GetComponent<StudyScreen>();


at this point anywhere in your script you can reference public stuff inside StudyScreen.cs by doing

sc.yourMethod();


or

sc.yourField;


Create a public variable and make it's type the name of the class you want to access so then just assign the script with the class to your variable in hierarchy and after that you can access the variables and functions in the class. Hope that helps.