How to call a FUNCTION FROM A DIFFERENT SCRIPT in Unity

preview_player
Показать описание
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
Рекомендации по теме
Комментарии
Автор

Could you have used other names than just "A" and "B"? It gets a bit confusing because the game object is called A but the script too and also the variable and everything and I don't know which A is the game object and which is the script and stuff

RadioactiveBluePlatypus
Автор

I'm so grateful. Other tutorials were leaving out the part where you define what a is. You're a lifesaver

LukeFlavel
Автор

You're a real life saver I was getting so angry because I couldn't figure it out. Thanks so much! :D

AboutLeon
Автор

very clear description bro.just improve mic Quality.❤

biki_
Автор

My first big problem in unity, but you solved it. Thank you ;)

Elenyen
Автор

Great! Straight to the point! Thanks for this!

nishitakulkarni
Автор

thanks a lot, its a giant leap for me

DerWiNG
Автор

Thank you very much, I don't understand why when we declare/instantiate the object based on class A it doesn't automatically get the characteristics from class A. the Null error was making me mad!!!

alonelyobserver
Автор

So helpful, much appreciated good sir!

anujpartihar
Автор

Resizing your visual studio font size would be great! Having issues identifying the tiny letters

loopmusic
Автор

Thank you so much I am using this for my game

abcdefghjijklfgu
Автор

Great video thank you, this helps a lot! ~

satobi
Автор

Hey,
Just a heads up for the future. Could you possibly increase the font size in the script so the code takes up the entire window. It's almost unreadable on mobile at 1080p

iamsecrets
Автор

thankyou very much, this is solve my problem

bachtiararyahabibie
Автор

Simple and understandable method, but I would avoid tags, because they are a strings, and strings have many problems: easy to make a typo, hard to find that typo and the console will tell nothing. I think it's better to find the gameobject with the A script. Code:

gofrisuto
Автор

Amazing video. This worked for me however only for one of my objects that had the script. I want this to work for every object in my scene that has the same script. Is there a way to do that or am I doing something wrong

efaz
Автор

thx bro thankfully you mention the "s" i got i wrong the first try hehehhe

ABYZA
Автор

How do you do that if youre accessing another script in another gameobject of another scene?

suzerain_k
Автор

Gracias, me funciono bien.... gracias

mxg
Автор

Awesome managed to use this to solve a problem in my own project, so THANKS. BTW, your debug.Log statement says " ....from A", but in the second case where your method was called from Script B, it ought to say " ...from B" :(

macleodgordon