How to get a variable from another script in Unity (the right way)

preview_player
Показать описание

Learn how to get a variable from another script in Unity, without making a mess of your project.

00:00 Intro
00:37 Creating a connection in the Inspector
02:06 Get Component
06:32 Connecting to different objects
09:09 Global variables
10:36 Global variables (using Singletons)
12:24 Global Variables (using Scriptable Object)
15:10 Outro

You'll learn how to connect objects and scripts together using the Inspector, Get Component, static variables and Singletons and by using scriptable objects.

Outro music: New Year by Bad Snacks

Image Attribution

Рекомендации по теме
Комментарии
Автор

Turns out you can also use Transform.root as an alternative to GetComponentInParent, it gets the topmost transform of an object hierarchy, or the target object if there's only one. It's probably going to be slightly more efficient than Get Component in Parent and is a good way to get a component from the root object if you know that's where it's going to be.

GameDevBeginner
Автор

The way you explain this so clearly and concisely makes me think you put an incredible amount of work into these videos. Either that or you are a God damn genius. Well done.

black_squall
Автор

I subscribed your channel right after I saw this video! I really love the detailed explanations which cover many different solutions with notes and alternatives.

jxqycxw
Автор

This explanation of ScriptableObjects is the first one I've seen that makes any sense.

SonOfSofaman
Автор

Amazing video, I was searching today for a solution to this problem and was surprised at the little amount of views your video had only to realize that this was uploaded today! Excellently done and thank you for explaining everything in a manner that was easy to understand. Subscribed!

csaurus
Автор

So much information packed into just one video! I learned so much! I'm excited to check out more of your videos :)

HalTime
Автор

This was such an ideal video to watch for me where I'm at. Awesome work, thanks for this.

brianwilson
Автор

Thanks for the tutorial, I'm doing a project for university in AR and this really helped me, it's my first with unity ... Thanks a lot again !!

arkelsean
Автор

I appreciate the effort you put into your videos man, keep it up!!

drn
Автор

Thank you so much. This video solved the problem that I was stuck on for days in a few minutes. You got yourself a new subcriber.

sipsopgamera
Автор

Literally my new favorite Unity developer source on YouTube

Ironlionmn
Автор

You are so amazing at explaining.. please don't stop!

Tetribution
Автор

Hey just wanna say ur video is amazing and im so glad i somehow stumbled upon ur blog. This helped me ALOT when i needed it the most.

randomworkshiet
Автор

Thank you, your video was the only one that worked for me

HumbertoOriginal
Автор

Thank you. I was to dumb to write public in front of the reference to the class. Thank you thank you thanks you. Keep up the good work you got yourself a new subscriber. :)

mrepi
Автор

Thanks a lot! Good talk about clean code, too.

mannig.faltigkeit
Автор

I use two approaches to managing variables:

1. A data script where I push all my major variables to (i.e. cash, progress, etc.). This is done via a Singleton from a controller script (i.e.
2. Using Game Managers to manage a set of game objects along with their respective variables (level, cost, etc).

I'm only about 6 months into learning Game Dev so going to Exploring incorporating Scriptable Objects a bit more after this video.

mortum
Автор

This helped my coding block!!! THANK YOU SO MUCH!

nobodycares
Автор

Yo! You saved my day by making the-

-the private variable to public instead. That's why my script says error haha.

creepermods
Автор

Man, you are awesome! Please, go on!

ttvreqo