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

GDT Solutions
GDT Solutions
18.2 هزار بار بازدید - 2 سال پیش - In this video we see
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.

____________________________________________________________
LINKS

Portfolio: https://gamedevtraum.com/en/portfolio...

LinkedIn: LinkedIn: gamedevtraum

Downloads: https://gamedevtraum.itch.io/

Contact: Instagram: gamedevtraum
____________________________________________________________
2 سال پیش در تاریخ 1401/10/15 منتشر شده است.
18,230 بـار بازدید شده
... بیشتر