Can someone explain calling other scripts in C#?

Hi, Unity and programming noob here. I’ve been working with a bunch of tutorials and there’s this thing that is popping up that I don’t quite understand. I was hoping someone could explain it to me in ELI5 terms.

So I understand what this is when programming:

public int score;

This variable contains an integer called “score” that has been declared but not initialized; later, I could do something like “score = 5;” and it will set score equal to 5.

Now suppose I have a script by the name of “Dialogue”. I set up a couple of variables and things in there, and then in another, separate script I do this:

public Dialogue dialogue;

What is this? I understand that the “public int score” is a variable that holds an integer by the name of score; is this other thing a variable? What does it hold, all of “Dialogue”? Does it just allow me access to all the items in “Dialogue” from this other script? Why do I have to rename it “dialogue”? Is this concept called “Composition” or “Inheritance” or something (I’ve heard of those but I don’t know what they are)? I’m just a bit confused and if someone can break this down for me in a simple manner I would really appreciate it.

Dialogue is a variable just like the int. You are holding memory for an object of type dialogue instead of type int.

If you have that reference and try to use it like: dialogue.DoSomething() you will get an error because it is just a reference that hasn’t been set to anything.

To assign an instance of the class you would do something like:

dialogue = new Dialogue();

However the confusing thing is that if your class extends MonoBehaviour then you can’t manually create a new Dialogue. You instead are using Unity’s built in component system. So to create it you would add it to a GameObject in the Unity Editor, then drag it into the dialogue variable in the inspector.

Unity basically built a drag and drop system for connecting components and variables of classes for convenience.

Calling a Script from a diffrent script/class can be usefull but also tends to be odd in some cases.
Lets say you have a UIController Script, that does everything to do with the UI itself, and another mainscript that does the rest of the work.
Lets say your Mainscript wants to tell the UIController that it needs to change active panels, like player presses a button to open the Options Panel, this is run in your UIController, but your Mainscript wants to set it up.

So in the UIController, lets say there is 2 Panels, a Main, and a Options Panel, and a function to change what is active. Here is a example UIController script


using UnityEngine;

public class uiCont : MonoBehaviour
{
    public GameObject optionsPanel; // 0
    public GameObject mainPanel; // 1

    private uint ActivePanel = 1;
    private bool PanelChange = true;

    void SetActivePanel()
    {
        if (ActivePanel == 0) { optionsPanel.gameObject.SetActive(true); } else { optionsPanel.gameObject.SetActive(false); }
        if (ActivePanel == 1) { mainPanel.gameObject.SetActive(true); } else { infoPanel.gameObject.SetActive(false); }
        PanelChange = false;
    }

    public void ChangePanel(uint newPanel)
    {
        ActivePanel = newPanel;
        PanelChange = true;

    }

    void Update()
    {
        if (PanelChange) { SetActivePanel(); }
    }
}

So In this case, in the UIController we setup 2 panels in the Inspector, so we drag the Options and Main panels over to the inspector to set them up. Now If the ActivePanel is 0 it wants to load the OptionsPanel and makes it Active and sets the MainPanel Inactive or off. If we want to change the Panel that is active, we have to some how call that ChangePanel function from inside the UIController.

Now in our Main Script, something like this:


using UnityEngine;

public class mainGame : MonoBehaviour
{
    private uiCont uiController;
    void Start()
    {
        uiController = gameObject.GetComponent<uiCont>();
        uiController.ChangePanel((uint)1);
    }
}

Now we create a refrence to that other script, and make a var of type uiCont since that is the name of the other scripts class, and it should See it and auto fill in while typeing if done right, but the private makes a object you can some what talk to. But if you try to run a function in it, it wont work because while its there, its not linked to the component. In the Start() function, it first sets that uiController we made to = the gameObject.GetComponent() where uiCont is the name of the script itself your wanting to attach to. This assumes Both the Main and the uiCont scripts are on the Same Game Object so has to be attached otherwise need to set it up diffrently.
uiController.ChangePanel((uint)1);
calls the uiCont.cs function called ChangePanel() that was created to handle changing active panels so this calls it up and says to change the current panel over to this number, that function then resets the panel id to 1 and tells it that it has changed.
The next Update called in the uiCont script will see the panel changed, and then reset what it shows.

Now if your trying to just handle variables that is diffrent and dosent need to attach the compnonents to.
If you have for example a data.cs that holds all the stuff then you just refrence what is created already.
So if I have a data.cs with a public struct SomeStruct { int someVar; } and I want to set it up to use it inside a diffrent script, I would make a new instance of it, like public data.SomeStruct myNewStruct = new data.SomeStruct [1024];
In a sence to create that new struct you just tell it the name of the file/script its in, in this case data.cs so we call data.NameOfVarible/Class/Struct we want, then as usuall give it a name.
You can also have it access other vars from functions with returns, like making a get function to display
or return the contents of a variable it has access to.
Just have to remember that making a New instance of something is a new version it dosent link it to another version somewhere else so if you assign a varible in one script and make a new varible of in this case a struct in another script, there not the same and hold diffrent data. So have to be carefull not to link to what you want anew version of or to otherwise change varibles you dont want to change.