How to REFERENCE ANOTHER SCRIPT in Unity - Using functions from other scripts

preview_player
Показать описание
In this video we see how to refernce another script in Unity, in particular how to call a function that is defined in another script in Unity. To do this we need to solve two different problems, the first one is how to access a function that is defined in another script, for that we need to work with an object of the type corresponding to the script in which the function we want to call is defined.
To fully understand this we need to abstract ourselves of the particular examples and try to analyze the generic procedure, that's why I used names "A" and "B" for our scripts, maybe it's a little confusing but doing so I tried to came up with a generic example.

Let's review the process with another example to complement this video.
Imagine that we have a function called "MyFunction" defined inside a script called "MyScript", then in any other script we can define a "MyScript" type variable and then use that variable to access to "MyFunction". We can achieve this by defining a variable like this:

//C#
public MyScript myScriptVariable;
//end C#

With the previous instruction we defined a variable called "myScriptVariable" whose type is "MyScript", then in the Start method for example we can use that variable to call the function that is defined in "MyScript", for example so:

void Start(){
myScriptVariable.MyFunction();
}

Assuming that "MyFunction" doesn't take input parameters.

UP UNTIL HERE WE HAVE THE HALF OF THE WORK DONE.

With the previous information we solved our first problem which is how we can access a function that is defined in another script, but there is another problem.
By default "myScriptVariable" has a NULL VALUE, that is to say is like a container with nothing inside. At this point if we press play we will get a NULL REFERENCE EXCEPTION, because we are trying to call a function to a null object. So we need to fix this.
Then the second problem is how we initialize our object variable ("myScriptVariable" of type "MyScript").
We can do this in many ways, to give you a couple examples:
-You can drag the GameObject which has the "MyScript" component assigned to the "myScriptVariable".
-You can do it by code finding directly the "MyScript" type component present in the scene.
-If you have the reference of the GameObject which has the MyScript assigned, you can perform a GetComponent to get the reference of the MyScript component.
-You can find a GameObject by it's tag, if you assign a certain tag to the game object that has the MyScript component assign, you can find it and then perform a GetComponent. This is the method that I used in this video.

____________________________________________________________
____________________________________________________________
Рекомендации по теме
Комментарии
Автор

Thank you, I was searching for how to use functions from another script without using inspector for a week.

exelezel
Автор

Great short approach, straight forward and on point !
This is what I needed.
Subscribed ❤

anthonyzornig
Автор

A weird thing thats happening to me is that when I change the function in one script, the other doesnt update it. I have a function Addscore(int addedScore) that is used in the other script as addscore();

Lost_S
Автор

So I couldn't seem to pass a variable from my main script to my auxiliary script. It resulted in a stack overflow. However the call function is triggered by ontriggerentry() so it should only send it once.

I got around this by editing a variable in my auxiliary script via my main script. E.g in main script auxScript.myVarb = valueIWantedToPass
Then calling the function without passing a variable.

Any ideas why this didn't work? Should I maybe be passing by reference rather than value?

iamsecrets
Автор

Good video man u saved my life! Cheers

aimanmohdsaid
Автор

My problem is, that the tag doesnt appear until 3 seconds after, so i get the nullpointerexception. I spawn the enemies after a couple of seconds aka the tag doesnt exist in the start

lordwigstyle