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

FindObjectOfType

  • 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

FindGameObjectWithTag

  • 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

Chain Of Responsibility Pattern

What are the best Coding Languages for beginners to learn in 2019?

Kotlin and the rise of Multiplatform

HackTheBox - Admirer WalkThrough

Why removing features in products makes sense

Linux for Programming

AXT VALUE

AWS CLI commands for DynamoDB

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

My life as an indie developer

Image by <a href=”https://pixabay.com/users/kellepics-4893063/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2847724">Stefan Keller</a> from <a href=”https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2847724">Pixabay</a>

Begin your C# Game Development Journey Using This Challenge

How to format a double value according to culture-specific formatting in Unity C#

ON YOUR MARKS…