filmov
tv
How to call a FUNCTION FROM A DIFFERENT SCRIPT in Unity
Показать описание
In this video we'll see how to access from a script to a method that is defined in another script in Unity.
How to use functions that are in another Script:
1. We start with 2 scripts A and B. In A Script is the function we want to call. In B Script we will call the function that is in A.
2. In A Script we make sure that the function or method is declared as public, otherwise we will not be able to access it from an external context to A.
3. In B script we declare an object of A type and we must find the reference of that object. This will depend on where we are programming, in the case of Unity we will do it in the Start method, with the instruction GameObject.FindGameObjectWithTag() and to that GameObject we execute the GetComponent method. The final instruction looks like this:
a=GameObject.FindGameObjectWithTag().GetComponent{A}(); //(replace the curly brackets with the signs less than and greater than).
4. To call the function of the other A Script from B, we use the A reference that we define and with the dot operator we can access that function and use it inside the B Script.
____________________________________________________________
*LINKS*
____________________________________________________________
#EN
How to use functions that are in another Script:
1. We start with 2 scripts A and B. In A Script is the function we want to call. In B Script we will call the function that is in A.
2. In A Script we make sure that the function or method is declared as public, otherwise we will not be able to access it from an external context to A.
3. In B script we declare an object of A type and we must find the reference of that object. This will depend on where we are programming, in the case of Unity we will do it in the Start method, with the instruction GameObject.FindGameObjectWithTag() and to that GameObject we execute the GetComponent method. The final instruction looks like this:
a=GameObject.FindGameObjectWithTag().GetComponent{A}(); //(replace the curly brackets with the signs less than and greater than).
4. To call the function of the other A Script from B, we use the A reference that we define and with the dot operator we can access that function and use it inside the B Script.
____________________________________________________________
*LINKS*
____________________________________________________________
#EN
Комментарии