Script communication is important and inevitable

How can you get access to a variable from another script?

Photo by Volodymyr Hryshchenko on Unsplash

Script communication is important in coding. It allows us to get access to another game object’s components (Variables and methods must be public if they are private. You can’t get access to those from another script.)

So let’s get to it! :) I will show here two ways you can do it using GetComponent


  • GameObject.FindObjectOfType<NPC>();

We have our Player Script and NPC Script where is the information we want.
First, we need something where we can store our information about another script

Then in Start() we write this code to get access to our NPC GameObject

Inside the NPC, I created an NPC script which has public int coins = 10;

And now in our Player script, we want to decrease the coin amount when we click our mouse(0). (Left mouse button)

Decrease the coin amount when clicking left mouse button


  • GameObject.FindGameObjectWithTag(“Bob”);

And now we do the same with tags! :)

All we need to change here is our start function and what we wrote inside the if statement.

Remember to add Tag in your game object which you are trying to get access to! In my case, I added a tag called Bob

We need to create a GameObject to hold the information

Our start should look like this

And our if statement in Update should look like this

Now we can do the same thing, decrease the coin amount by clicking! :)
Steal Bob’s money! :D

Thank you for reading this, Get Component has always been a bit off over my head subject. But it’s a really interesting subject and I recommend learning more about it.

Much love and see you in the next one! :)

Photo by Nathan Dumlao on Unsplash




From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Go Data Structures: Implementing stack using Golang


Top FAQs on SaaS

The technology to support SaaS came around during the 1990s.

Apache Spark 3.0: Remarkable Improvements in Custom Aggregation

An Introduction To The CKEditor And Text Formats In Backdrop CMS

Why I Love Software Testing

How to avoid unicodedecode error in a csv file.

Java Modularity

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
Janika Suhonen

Janika Suhonen

From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

More from Medium

From Prototype to Work of Art, What is it all about?

Day 17 of my Developer Journey: Creating the Speed Boost Powerup

TDD: Testing Testing… 1 2 3…