How to Run Scripts in Unity: 2 Methods
In Unity, there are typically two ways to make scripts run.
- Attach the script to a game object: Drag and drop the written script file onto the game object in Unity’s Hierarchy panel, which will then attach the script to that game object. When the game is running, the script will start executing.
- Call scripts in code: Call the script that needs to be executed in other scripts through code. For example, use the GetComponent method to retrieve the corresponding script component, and then call its methods or properties.
Regardless of the method used, as long as the script is properly mounted or called, the corresponding code will start executing during the game’s runtime.