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

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ย. 2024

ความคิดเห็น • 75

  • @GameDevBeginner
    @GameDevBeginner  2 ปีที่แล้ว +28

    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 10 หลายเดือนก่อน +10

    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 3 หลายเดือนก่อน +1

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

  • @냥만-p2e
    @냥만-p2e 2 ปีที่แล้ว +17

    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.

  • @csaurus526
    @csaurus526 2 ปีที่แล้ว +7

    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!

  • @HalTime
    @HalTime ปีที่แล้ว +3

    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!

  • @Ironlionm4n
    @Ironlionm4n ปีที่แล้ว

    Literally my new favorite Unity developer source on TH-cam

  • @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 !!

  • @drn1850
    @drn1850 2 ปีที่แล้ว +6

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

  • @Tetribution
    @Tetribution 2 ปีที่แล้ว +4

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

  • @brianwilson7497
    @brianwilson7497 8 หลายเดือนก่อน

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

  • @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.

  • @Arquivo_Tech
    @Arquivo_Tech 9 หลายเดือนก่อน

    This is the best Unity channel on youtube

  • @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. :)

  • @creepermods4641
    @creepermods4641 ปีที่แล้ว +1

    Yo! You saved my day by making the-
    -the private variable to public instead. That's why my script says error haha.

  • @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.

  • @nobodycares9579
    @nobodycares9579 7 หลายเดือนก่อน

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

  • @alexleonardkrea
    @alexleonardkrea 2 ปีที่แล้ว +2

    Fantastic Video!

  • @HumbertoOriginal
    @HumbertoOriginal ปีที่แล้ว

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

  • @Ferundal
    @Ferundal ปีที่แล้ว

    One of the best videos i ever saw!

  • @Oscorky
    @Oscorky 10 หลายเดือนก่อน +1

    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  10 หลายเดือนก่อน

      Absolutely! th-cam.com/video/J01z1F-du-E/w-d-xo.html

  • @Chocoletter
    @Chocoletter ปีที่แล้ว

    bro you are fucking amzing i was stuck on this for 3 days.

  • @mannig.faltigkeit2341
    @mannig.faltigkeit2341 ปีที่แล้ว

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

  • @Ingy-d7t
    @Ingy-d7t ปีที่แล้ว

    Man, you are awesome! Please, go on!

  • @whiteline4157
    @whiteline4157 ปีที่แล้ว

    super cool stuff. i have been doing it wrong all this time

  • @yasser_gg2276
    @yasser_gg2276 6 หลายเดือนก่อน +1

    all whaaat i can said is thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaank youuuuuuuuuuuuuuuuuuuuuuuuuuuu 🥰

  • @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

  • @andrewsgates4040
    @andrewsgates4040 10 หลายเดือนก่อน

    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;

  • @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?

  • @ssshammi
    @ssshammi 7 หลายเดือนก่อน

    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!

  • @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.

  • @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.

  • @humanbeing366
    @humanbeing366 ปีที่แล้ว +1

    really good

  • @watercat1248
    @watercat1248 2 ปีที่แล้ว

    I have a question it's possible to add script or Chenge the component in a instead game object ?

  • @the_allay_4846
    @the_allay_4846 2 หลายเดือนก่อน

    dude all i wanted was to access one varaible from a diffrent script and it doesnt tell me

  • @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 ปีที่แล้ว +1

      Good point about statics! Thanks for adding that.

  • @JotaaPlays
    @JotaaPlays ปีที่แล้ว +5

    I cant drag the script to the other, it doesnt let me, i did exactly what you did...

    • @FP-ih1lu
      @FP-ih1lu 2 หลายเดือนก่อน +1

      Try dragging the GameObject that the script is attached to

    • @JotaaPlays
      @JotaaPlays 2 หลายเดือนก่อน

      @@FP-ih1lu thanks man, since last year I have learned a lot and did it like 100 times xD

    • @FP-ih1lu
      @FP-ih1lu 2 หลายเดือนก่อน

      @@JotaaPlays I figured you didn’t need this anymore but just thought I’d reply in case someone else saw lol

  • @nightmarevstplugin3944
    @nightmarevstplugin3944 2 ปีที่แล้ว

    Thanks

  • @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  ปีที่แล้ว +1

      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!

  • @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.

  • @jarrettonions3392
    @jarrettonions3392 11 หลายเดือนก่อน

    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  11 หลายเดือนก่อน

      Thanks! I might try that

  • @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  ปีที่แล้ว +1

      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.

  • @mortum2614
    @mortum2614 2 ปีที่แล้ว +1

    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.

  • @pigtech1872
    @pigtech1872 ปีที่แล้ว

    Hey mate. What theme do you happen to be using? Love the colors.

    • @GameDevBeginner
      @GameDevBeginner  ปีที่แล้ว

      I think it's Gruvbox

    • @pigtech1872
      @pigtech1872 ปีที่แล้ว

      @@GameDevBeginner Appreciate it. Solid theme

  • @eren8943
    @eren8943 ปีที่แล้ว

    so what is the right way ?

  • @Lemon-dh4fz
    @Lemon-dh4fz หลายเดือนก่อน

    DI! DI! And only DI!!!

  • @carlgranstrom6827
    @carlgranstrom6827 ปีที่แล้ว

    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.

  • @MRJakeyBro
    @MRJakeyBro ปีที่แล้ว

    i then used trial and error to change a floats value in it

  • @teimurlu
    @teimurlu ปีที่แล้ว

    This was quite helpful. Thanks!

  • @pedropc5824
    @pedropc5824 2 ปีที่แล้ว

    Your blog and channel are very helpful. I've never heard anything about "otherObject.GetComponent". Thanks!!!