In Unity game development, scripts are essential for adding behavior to game objects. Often, you’ll need to communicate between different scripts—such as calling a function from one script in another. This tutorial walks you through the process step by step, explaining key concepts like references, instances, and visibility. By the end, you’ll understand how to call functions between scripts.
Prerequisites
- Basic knowledge of C# scripting in Unity (e.g., defining functions and using Debug.Log).
- A Unity project set up with an empty scene.
In the following video I show an example on how to call a function defined in another script in Unity in C#:
Step 1: Creating the Scripts
Start by creating two new C# scripts in your Unity project:
- Right-click in the Assets folder, select Create > C# Script, and name it ScriptA.
- Repeat the process to create ScriptB.
Open both scripts in your code editor (e.g., Visual Studio).
ScriptA: Defining the Function
In ScriptA.cs, define a simple function that we’ll call from ScriptB. We’ll also add some logging in the Update method to demonstrate script execution. In the image below we define a function called “FunctionToCall”, which is the function that is going to be called from the other script.

- Key Point: The function is marked as public to make it accessible from other scripts. Without public, it defaults to private and won’t be visible externally.
ScriptB: Calling the Function
In ScriptB.cs, we’ll declare a reference to ScriptA and call its function.

- Here, otherScript is a variable of type ScriptA. Using the dot operator (.), we access and call FunctionToCall().
Step 2: Understanding Script Instances and References
Scripts in Unity don’t run on their own—they need to be attached to GameObjects in the scene to create instances. Each instance is independent.
Why Instances Matter
- Attach ScriptA to a GameObject (e.g., the Main Camera).
- Play the scene: You’ll see messages from Update() in the console only if the script is instanced.
- Attach ScriptA to multiple GameObjects: Each instance runs separately, logging its own GameObject’s name.
Example: If you have two GameObjects with ScriptA attached (e.g., “Main Camera” and “Enemy”), calling FunctionToCall() requires specifying which instance to target. Unity won’t guess—you must provide a reference.
Common Error: Null Reference Exception
If you try calling the function without initializing otherScript, you’ll get a Null Reference Exception. This means the variable is empty (null) and points to nothing.
Step 3: Setting Up the Scene
- Create two empty GameObjects in your scene:
- Name one ScriptA_Object.
- Name the other ScriptB_Object.
- Attach ScriptA to ScriptA_Object.
- Attach ScriptB to ScriptB_Object.
Step 4: Initializing the Reference
To fix the null reference:
- Make the otherScript field in ScriptB public (already done in the code above).
- In the Unity Editor, select ScriptB_Object.
- In the Inspector, drag ScriptA_Object into the Other Script field of ScriptB.
This assigns the specific instance of ScriptA to the variable.
Step 5: Testing the Call
- Play the scene.
- Check the console: You should see repeated messages like “Hello! Function from ScriptA called. That’s amazing! I am: ScriptA_Object”.
- Why Repeated? The call is in Update(), which runs every frame.
- Tip: In the Console window, enable Collapse to group identical messages.
Handling Multiple Instances
Duplicate ScriptA_Object and rename it (e.g., “John”).
- If you drag the original ScriptA_Object to ScriptB’s reference, the logs will show “I am: ScriptA_Object”.
- Drag “John” instead: Logs change to “I am: John”.
This demonstrates targeting specific instances. In a game, this is crucial—for example, applying damage only to the hit enemy, not all enemies sharing the same script.
Key Concepts Explained
- References: A reference is like a pointer to a specific script instance. Without it, you can’t access functions or variables.
- Visibility: Use public for functions/variables you want accessible from other scripts.
- Multiple Instances: Scripts can be attached to many GameObjects. Always initialize references to target the correct one.
- Alternative Initialization Methods: Beyond dragging in the Editor, you can use code like FindObjectOfType<ScriptA>() or GetComponent<ScriptA>() for dynamic referencing (not covered here for simplicity).
Potential Challenges
- Null References: Always check if a reference is null before using it (e.g., if (otherScript != null)).
- Performance: Calling functions every frame (in Update()) can be inefficient—use events or coroutines for better optimization.
- Advanced Scenarios: For complex projects, consider ScriptableObjects or singletons for shared logic.
Conclusion
Calling functions between scripts in Unity is straightforward once you grasp instances and references. Start with the code-level setup (defining public functions and variables), then handle the Unity-side initialization. This technique is foundational for building interactive games, like enemy AI or UI interactions.
Experiment in your project, and remember: Practice with multiple instances to solidify the concept. If you found this helpful, explore more Unity tutorials on inter-script communication!