How to Pass Variables Between Scripts in C#

How to Pass Variables Between Scripts in C#

In this article, you are going to learn how to pass variables between scripts in C# and in Unity. This is highly common and extremely important so it’s a fantastic idea to get a solid grasp of this concept as early on as possible in your programming career.

In this example, what we want to do is drop a ball onto a paddle, and increment the score text in the upper-right corner of the screen by 100 points every time the ball bounces.

Example

Easy enough, right? Let’s take a look at the code and how we can do this by passing variables. Before we do that, let’s take a look at what we have in our scene:

  1. On the paddle, we have a C# script called Paddle.cs.
  2. On an empty GameObject called Gameplay Controller, we have a script called GameplayController.cs
  3. We have an empty text element called “ScoreText” without any scripts attached.

We want the paddle (which contains the score value) to tell the Gameplay Controller to add 100 points to the Score Text whenever the ball bounces off the paddle.

Inside Paddle.cs, we write a new function to register a collision. It looks like this:

OnCollisionEnter2D is the name of the function we write to register a new collision. It takes in a “Collision2D’ which we have assigned the name “collision.” Inside of this variable named “collision” is all the data surrounding the collision which has just occurred.

Next, we write the following inside of our GameplayManager.cs script:

Let’s go over this line-by-line and explain what is happening.

  • We declare two variables; one is a Text variable called scoreText. This has been made public and in the Unity Inspector, we drag the text element onto that variable.
  • The other variable is called currentScore. It is an integer and it is what we use to keep track of our game’s score.
  • Next, we have declared a public function named UpdateScore. We have made it public so we can call it from another script (in this case, our Paddle.cs). Inside the brackets, we have declared a new variable “int score.” This is the “receiving” end of passing variables.

  • When we call UpdateScore from our paddle script, we are going to “pass” the value of our score variable to the GameplayController.cs script. score is the name of the variable that is going to receive the value.

Now that we’ve created this, let’s go back to Paddle.cs and finish the script. Here’s what the rest of it will look like:

Now, we have created a score variable in Paddle.cs and assigned it an inital value of 100 points. We have then obtained a reference to our GameplayManager script, and finally, we call the UpdateScore method by writing gameplayManager.UpdateScore (score);

This last bit is very important. This is the other part of passing a variable between scripts. In the brackets we have written “score,” which is the name of the variable that passes the data contained within “score” (100 points). In GameplayManager, we have declared a separate variable, also named “score” (but it could be called anything we want) to catch and use that data within GameplayManager.cs.

This is how we pass variables between scripts.

Still confused? Check out the video below which goes hand-in-hand with this tutorial. Questions or comments? Please leave one below! We will get back to you 🙂

By | 2017-06-14T00:20:57+00:00 June 14th, 2017|Tutorials|Comments Off on How to Pass Variables Between Scripts in C#

About the Author:

Jonathan is the Executive Producer at Wild Cockatiel Games. He's an avid gamer and currently loves playing Overwatch. He's also an author seeking publication for his YA fantasy novel, Elementalists: The Fires of Canicus.