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.