I have an “InputScript” which manages the player’s keyboard/mouse input, and it is the same for each scene.
I want it to call functions from the “main script” from each scene. Those “main scripts” are one single different script for each scene, and each one has obviously a different name (the name of the scene + “Script”).
The thing is, each of them has a funcion called “PressedA()” that will have a different content depending on the script, but is always activated from the InputScript by pressing the A button. I thought of doing this:
public Script MainScript;
#I'd try to drag the main script of that scene there, but the box to drag it in just won't show up
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
MainScript.PressedA();
}
}
I also tried to rename all those “main scripts” as MainScript so I could reference them like “public MainScript MainScript;”, but being different scripts with the same name made Unity nuts and I had to change it all back.
You can use an Interface for this. An interface is like a contract for a class that guarantees that class will contain a method with a certain name. First, define your interface (in its own file):
interface IMainScript {
void PressedA();
}
Then, each of your scripts that has this function should implement the interface:
public class OneOfMyMainScripts : IMainScript {
public void PressedA() {
// Whatever the specific implementation of PressedA is for this class.
}
}
Then, in your other scripts you can call the function as follows:
public IMainScript MyMainScript;
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
MyMainScript.PressedA();
}
}
So, the issue is you have a MainScript that is different for every scene. It’s difficult to know why you need this without seeing what you are doing. I highly doubt you do. Scripts when setup correctly are generally reusable. It’s the data that should be variable so that you have a script that acts on a given set of data.
But, if we assume that you can’t do that, you need to look at two possible options. Either interfaces or inheritance would be my suggestion. You could probably also set up events and have the scripts subscribe to the event, which could also work.
I supposed that I was doing it wrong because of the results I was getting, but I don’t know how to do it in any other way. The game is a 2D adventure with mini-games, so each scene works in a different way. Those “main scripts” have the orders that make the scene run: what do the characters do, how they react, what do they react to, etc. It’s like the script of a theatre play. That’s why I had to make them different in each scene.
Oh geez, it sounds like your main script is doing way to many things. Main script should manage other scripts. Sort of behaving like a game manager. Then you can tie in other scripts into it. Again, the scripts should still act on the data.
Script of a theatre play is still data. If I tell you what line to read and where to stand, I’m providing you data, not controlling how you act on it. If I tell an actor to go to the left or right side of the stage, I’ve given them info on where to go. They should know how to get there using their own “pathfinding” for example. Even with directions (I want the actor to run or limp) it’s still data on how they should do things.
Code would handle what to do with that data. I’m provided a line on the script, my code executes to speak it or display the text on the screen.
These can be very hard concepts to grasp when starting out, which is why I suggested the interface, inheritance, or events. I’ve seen plenty of newer programmers hard code things or not understand the difference between data and the code that acts on it. And my explanation may not be the best. lol.