Passing Values from One Function to Another

Objective: Pass in a custom score value per game object to another object via a function.

Here’s how I made my score system work. What I care about is running my score direct 1–1, so when my enemy dies, I want it to talk to my UI Manager. To start, I have my Enemy connect to the UI Manager.

Next, I created a serialized field for the score value on my enemy.

Keep in mind, it has to be the same value of int. Then, I created a simple function that tells the function on my UI to run. I then made sure I pass the _score value vs simply leaving it empty like this: _UIManager.AddScore();

On the UI Manager, I created a public function called AddScore. Since I sent an int value in the step above, I have to make sure I can catch that variable amount in my UI manager. After declaring the function name, I created an int temp variable and called it points. It will take in the points that I send over from my enemy script. Then I added those points to my _score.

Finally after getting the text component on my UI, I simply update the score value.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Al Heck

Unity Developer and Software Engineer who loves making games with GameDevHQ