Skip to content

How to Access Another Script in Unity using C#

Accessing a script in a gameobject from another script in another gameobject

public class Object1Script: MonoBehaviour

{

     public GameObject object2WithScriptToAccess;

     public NameOfScriptToAccess nameOfScriptToAccessScript;

 

void Start()

     {

          nameOfScriptToAccessScript = object2WithScriptToAccess.GetComponent();

     }

 

void Update ()

OR

other method()

     {

          nameOfScriptToAccessScrip.enabled = true; //or false

          OR

          nameOfScriptToAccessScrip.NameOfMethodInThatScript();

     }

}

Leave a Reply

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