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

  Рет қаралды 101,943

Game Dev Beginner

Game Dev Beginner

Күн бұрын

Пікірлер: 76
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
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.
@lance7135
@lance7135 Жыл бұрын
I've found that depends on the scene structure, though. If I have a thruster manager that wants to look at the ship it belongs to, but that ship is part of a collection that is attached to a root object, assuming it is the root object would not find the specific ship that the thruster manager is looking for. It is faster, but one needs to be absolutely sure that is the object they need.
@black_squall
@black_squall Жыл бұрын
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.
@SonOfSofaman
@SonOfSofaman 7 ай бұрын
This explanation of ScriptableObjects is the first one I've seen that makes any sense.
@냥만-p2e
@냥만-p2e 2 жыл бұрын
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.
@charsaurus
@charsaurus 2 жыл бұрын
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!
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
Thank you!
@Ironlionm4n
@Ironlionm4n 2 жыл бұрын
Literally my new favorite Unity developer source on KZbin
@HalTime
@HalTime Жыл бұрын
So much information packed into just one video! I learned so much! I'm excited to check out more of your videos :)
@GameDevBeginner
@GameDevBeginner Жыл бұрын
There will definitely be more. I'm just slow at making them!
@Arquivo_Tech
@Arquivo_Tech Жыл бұрын
This is the best Unity channel on youtube
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you so much!
@arkelsean7336
@arkelsean7336 Жыл бұрын
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 !!
@mortum2614
@mortum2614 2 жыл бұрын
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. "Controller.instance.data.playerLevel++;") 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.
@drn1850
@drn1850 2 жыл бұрын
I appreciate the effort you put into your videos man, keep it up!!
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
Thanks so much!
@Tetribution
@Tetribution 2 жыл бұрын
You are so amazing at explaining.. please don't stop!
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
Thank you!
@brianwilson7497
@brianwilson7497 11 ай бұрын
This was such an ideal video to watch for me where I'm at. Awesome work, thanks for this.
@mrepi0334
@mrepi0334 Жыл бұрын
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. :)
@sipsopgamera7624
@sipsopgamera7624 Жыл бұрын
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.
@randomworkshiet6330
@randomworkshiet6330 2 жыл бұрын
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.
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
Glad I could help!
@creepermods4641
@creepermods4641 Жыл бұрын
Yo! You saved my day by making the- -the private variable to public instead. That's why my script says error haha.
@Ferundal
@Ferundal Жыл бұрын
One of the best videos i ever saw!
@Oscorky
@Oscorky Жыл бұрын
You could also use a design pattern such as Observer Pattern to send events between objects that way not everything is coupled together.
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Absolutely! kzbin.info/www/bejne/gGGUq2R8YsmoY6c
@alexleonardkrea
@alexleonardkrea 2 жыл бұрын
Fantastic Video!
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
Thank you!
@nobodycares9579
@nobodycares9579 10 ай бұрын
This helped my coding block!!! THANK YOU SO MUCH!
@HumbertoOriginal
@HumbertoOriginal Жыл бұрын
Thank you, your video was the only one that worked for me
@Chocoletter
@Chocoletter Жыл бұрын
bro you are fucking amzing i was stuck on this for 3 days.
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Happy I could help!
@MRJakeyBro
@MRJakeyBro Жыл бұрын
before i noticed the get component part later on in the video i had to go into the unity documentaiton to find stuff but i still got it working that way
@Ingy-d7t
@Ingy-d7t Жыл бұрын
Man, you are awesome! Please, go on!
@teimurlu
@teimurlu Жыл бұрын
This was quite helpful. Thanks!
@mannig.faltigkeit2341
@mannig.faltigkeit2341 2 жыл бұрын
Thanks a lot! Good talk about clean code, too.
@yasser_gg2276
@yasser_gg2276 10 ай бұрын
all whaaat i can said is thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaank youuuuuuuuuuuuuuuuuuuuuuuuuuuu 🥰
@whiteline4157
@whiteline4157 Жыл бұрын
super cool stuff. i have been doing it wrong all this time
@colinkeith3533
@colinkeith3533 17 күн бұрын
it said type or namespace player (the thing i'm trying to get a bool from) could not be found and while i try to set an assembly reference because of the error it wont show up
@andrewsgates4040
@andrewsgates4040 Жыл бұрын
thank you for the video please I see a lot of declaring variables using data types Please what are these examples of variables below. what is this variables called public Vector3 forward; public Transform parent; public Quaternion rotation;
@carlgranstrom6827
@carlgranstrom6827 2 жыл бұрын
I really like the scriptable objects approach, but it doesn't work well for multiplayer games where I don't know the amount of players I will spawn beforehand. I am using SOs for global event channels though(where I process combat events). For multiplayer I'm sticking with the "search once"-approach, although I could of course set up a sort of "playerjoined"-channel where player scripts "register" themselves by invoking an event with a reference to itself and then gets called back with events to rig them up to existing objects that it will need to communicate with. But I havn't thought that one through and I'm not sure I'll go there, I'm also quite new at Unity, and networking complicates things immensely.
@777paradoxable
@777paradoxable Жыл бұрын
What about dependency injection? In my experience, this is the best way to distribute logic and avoid unnecessary dependencies. Isn't the pattern common in Unity?
@Nirioonossian
@Nirioonossian Жыл бұрын
How to change variable value in the original script from the script that's referencing it? I want to change the starting position of a line renderer on the fly via collision detection and I've been failing for days.
@ssshammi
@ssshammi 10 ай бұрын
Hi i couldent reference StarterAssets or Cinemachine in another script to get serialize object of that type. How do i do that ?
@lukedittydooda2157
@lukedittydooda2157 Жыл бұрын
perfect, thanks!
@DannyBoy443
@DannyBoy443 Жыл бұрын
You give great explnations and I love your videos. But do you have a basic C#/Unity course? I'd love that
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Not yet, but if you / others would want that then I'll make one.
@pigtech1872
@pigtech1872 2 жыл бұрын
Hey mate. What theme do you happen to be using? Love the colors.
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
I think it's Gruvbox
@pigtech1872
@pigtech1872 2 жыл бұрын
@@GameDevBeginner Appreciate it. Solid theme
@watercat1248
@watercat1248 2 жыл бұрын
I have a question it's possible to add script or Chenge the component in a instead game object ?
@JotaaPlays
@JotaaPlays 2 жыл бұрын
I cant drag the script to the other, it doesnt let me, i did exactly what you did...
@FP-ih1lu
@FP-ih1lu 5 ай бұрын
Try dragging the GameObject that the script is attached to
@JotaaPlays
@JotaaPlays 5 ай бұрын
@@FP-ih1lu thanks man, since last year I have learned a lot and did it like 100 times xD
@FP-ih1lu
@FP-ih1lu 5 ай бұрын
@@JotaaPlays I figured you didn’t need this anymore but just thought I’d reply in case someone else saw lol
@humanbeing0000ZZZ
@humanbeing0000ZZZ 2 жыл бұрын
really good
@zak-ks7ee
@zak-ks7ee Жыл бұрын
Could i ask I if scriptable objects work across different scenes as well? If your player character need certain variables across many different levels, especially some that your player character game object may not be holding. Would scriptable objects be a good idea for carrying variables to other levels?
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Yes they would. So long as it's not a reference to a game object or a component that gets destroyed, scriptable objects carry their data between scenes.
@zak-ks7ee
@zak-ks7ee Жыл бұрын
@@GameDevBeginner thank you so much!
@MyPing0
@MyPing0 2 жыл бұрын
What a well made video! It should also be noted that static variables do not get displayed in Unity's insepctor, so Singletons are a great way to work around that. I use Singletons and Public variables way for often than Find methods. GetComponent is kind of unavoidable and very useful too! Also, thanks for including Scriptable Objects in this video, they are really hard to understand for me, at least their use cases. But when you explained the similarity between Audio Sources and Audio Clips, it made sense! Great in depth explanations as always :)
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
Good point about statics! Thanks for adding that.
@the_allay_4846
@the_allay_4846 5 ай бұрын
dude all i wanted was to access one varaible from a diffrent script and it doesnt tell me
@fluorescente
@fluorescente 2 жыл бұрын
I tried the static variable approach exactly like in your example, but it didn't work. It just says the name does not exist in current context
@GameDevBeginner
@GameDevBeginner 2 жыл бұрын
If you don't mind giving me more information, get in touch at gamedevbeginner.com/contact/ and I can take a look for you.
@omarkraidie
@omarkraidie Жыл бұрын
I have a public variable X in a script file attached to a game object. This game objects has nested game objects that needs to access the same variable how can I achieve this please help how is this so hard? :D
@GameDevBeginner
@GameDevBeginner Жыл бұрын
So I've actually got a video in the works that might help you with this but, for now, if your variable x is on your root object, other scripts can use GetComponent on transform.root, i.e. they don't have to search up through the hierarchy.
@jarrettonions3392
@jarrettonions3392 Жыл бұрын
You should set the background of your profile pic to the same yellow, even if you change the ; to grey or black. It would stand out more. I like that its an info i and a coding ;
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thanks! I might try that
@nightmarevstplugin3944
@nightmarevstplugin3944 2 жыл бұрын
Thanks
@eren8943
@eren8943 2 жыл бұрын
so what is the right way ?
@MRJakeyBro
@MRJakeyBro Жыл бұрын
i then used trial and error to change a floats value in it
@Lemon-dh4fz
@Lemon-dh4fz 4 ай бұрын
DI! DI! And only DI!!!
@pedropc5824
@pedropc5824 2 жыл бұрын
Your blog and channel are very helpful. I've never heard anything about "otherObject.GetComponent". Thanks!!!
Connecting scripts on the same object in Unity
15:36
Game Dev Beginner
Рет қаралды 12 М.
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
The Power of Scriptable Objects as Middle-Men
17:41
samyam
Рет қаралды 132 М.
The right way to pause a game in Unity
9:09
Game Dev Beginner
Рет қаралды 63 М.
Better Coding in Unity With Just a Few Lines of Code
15:27
Firemind
Рет қаралды 318 М.
How To Access Variables From Another Script In Unity
8:07
Infallible Code
Рет қаралды 116 М.
How To Render 2 Million Objects At 120 FPS
14:57
Tarodev
Рет қаралды 151 М.
Is Unity free? (and will you ever need Unity Pro?)
15:09
Game Dev Beginner
Рет қаралды 14 М.
Dear Game Developers, Stop Messing This Up!
22:19
Jonas Tyroller
Рет қаралды 775 М.
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН