How to call FUNCTIONS defined in another Script. Examples in C# Unity

Introduction

In this article we will see how to execute functions defined in one script from a second script. First we look at the generic procedure.

Procedure

1. Let 2 Scripts A and B. In Script A is the function we want to call. In Script B we are going to call the function that is in A.

2. In A script we make sure that the function is declared as public, otherwise we will not be able to access it from a context outside of A.

3. In B script we declare a type A object 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 and with the instruction FindObjectOfType.

4. To call the function in the other A Script from B, we use the rererence we defined from A and with the dot operator we can access it and execute it inside Script B.

Leave a Comment

Your email address will not be published. Required fields are marked *