0

I'm working on creating a master script in Unity to control the other scripts. I'm stuck at one part which is the master script can call function from the others.

For example below is script B

public class B { void Test() { Dosomething; } } 

Script A will be the master one that control script B. Is there any way that you can call Test() in script B like A.Test()? I tried interface but then I still have to create another Test() function inside script A. This will be troublesome if I include more scripts with more functions.

Any advice would be appreciated.

4
  • 1
    public void Test() Commented Jul 22, 2019 at 2:18
  • Is the other script attached to another gameobject or the same one as the main script? Commented Jul 22, 2019 at 2:22
  • " Is there any way that you can call Test() in script B like A.Test()? " Can you make a example to explain what is you want ? Commented Jul 22, 2019 at 3:19
  • I know it's a bit confusing but this is the idea I want to implement. I have 3 scripts A, B and C. Script A is the master script, script B has a TestB() function and script C has TestC() function. Normally if you can call B.TestB() and C.TestC() but I want to call them by using A.TestB() and A.TestC(). Commented Jul 22, 2019 at 14:00

2 Answers 2

1

This is from the Unity tutorials. Should be a good start...

public class UsingOtherComponents : MonoBehaviour { public GameObject otherGameObject; private AnotherScript anotherScript; private YetAnotherScript yetAnotherScript; private BoxCollider boxCol; void Awake () { anotherScript = GetComponent<AnotherScript>(); yetAnotherScript = otherGameObject.GetComponent<YetAnotherScript>(); boxCol = otherGameObject.GetComponent<BoxCollider>(); } void Start () { boxCol.size = new Vector3(3,3,3); Debug.Log("The player's score is " + anotherScript.playerScore); Debug.Log("The player has died " + yetAnotherScript.numberOfPlayerDeaths + " times"); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I'm aware of this method but it's not quite what I'm looking for. Base on this script, for example, there is a function called Test() in AnotherScript and I want to call it using UsingOtherComponents.Test() without having to create another Test() in UsingOtherComponent and having it inherit the Test() from AnotherScript.
0

Okay, so the first things you should be aware about is that you really cant use static classes and functions in unity. That has something to do with every component on a gameobject essentially being a script the is instanced. So simply typing "using ScriptB" on the top and the calling the function like "ScriptB.Test();" really wont do anything.

What you need to do is to pass in a Script B reference as a variable in to Script A and then assign either drag it in editor or assign it via Script A. But in any case both scripts have to be in the same scene and assign to any game objects that are in that scene (they dont have to be on the same one)

So the final solution would look like this:

public class A { public B refToB; void Start() { //we call the function here refToB.Test(); } } public class B { public void Test(){ //we can do something here :) } } 

Only thing for this to work is thet you need to assign the variable refToB through the inspector panel in the editor. A simple drag and drop action.

Hope it helps :)

3 Comments

You absolutely can use static classes in Unity. They just can't be attached to GameObjects.
I mean you are right but in Unity you need to work with game objects 99% of the time
Have a Unity project that has a number of monobehaviour scripts I can count on one hand.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.