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

Поделиться
HTML-код
  • Опубликовано: 21 дек 2024

Комментарии • 76

  • @GameDevBeginner
    @GameDevBeginner  2 года назад +31

    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 Год назад +12

    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 6 месяцев назад +3

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

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

  • @냥만-p2e
    @냥만-p2e 2 года назад +18

    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.

  • @Ironlionm4n
    @Ironlionm4n 2 года назад

    Literally my new favorite Unity developer source on RUclips

  • @HalTime
    @HalTime Год назад +4

    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

  • @Tetribution
    @Tetribution 2 года назад +4

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

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

  • @brianwilson7497
    @brianwilson7497 10 месяцев назад

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

  • @drn1850
    @drn1850 2 года назад +6

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

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

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

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

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

  • @Ferundal
    @Ferundal Год назад

    One of the best videos i ever saw!

  • @nobodycares9579
    @nobodycares9579 10 месяцев назад

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

  • @alexleonardkrea
    @alexleonardkrea 2 года назад +2

    Fantastic Video!

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

  • @HumbertoOriginal
    @HumbertoOriginal Год назад

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

  • @Ingy-d7t
    @Ingy-d7t Год назад

    Man, you are awesome! Please, go on!

  • @teimurlu
    @teimurlu Год назад

    This was quite helpful. Thanks!

  • @Oscorky
    @Oscorky Год назад +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  Год назад

      Absolutely! ruclips.net/video/J01z1F-du-E/видео.html

  • @mannig.faltigkeit2341
    @mannig.faltigkeit2341 2 года назад

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

  • @colinkeith3533
    @colinkeith3533 6 дней назад

    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

  • @whiteline4157
    @whiteline4157 Год назад

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

  • @Chocoletter
    @Chocoletter Год назад

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

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

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

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

  • @lukedittydooda2157
    @lukedittydooda2157 Год назад

    perfect, thanks!

  • @yasser_gg2276
    @yasser_gg2276 9 месяцев назад +1

    all whaaat i can said is thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaank youuuuuuuuuuuuuuuuuuuuuuuuuuuu 🥰

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

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

  • @humanbeing0000ZZZ
    @humanbeing0000ZZZ 2 года назад +1

    really good

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

  • @watercat1248
    @watercat1248 2 года назад

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

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

  • @pigtech1872
    @pigtech1872 2 года назад

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

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

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

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

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

  • @nightmarevstplugin3944
    @nightmarevstplugin3944 2 года назад

    Thanks

  • @eren8943
    @eren8943 2 года назад

    so what is the right way ?

  • @JotaaPlays
    @JotaaPlays 2 года назад +5

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

    • @FP-ih1lu
      @FP-ih1lu 5 месяцев назад +1

      Try dragging the GameObject that the script is attached to

    • @JotaaPlays
      @JotaaPlays 5 месяцев назад +1

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

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

  • @MRJakeyBro
    @MRJakeyBro Год назад

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

  • @Lemon-dh4fz
    @Lemon-dh4fz 3 месяца назад

    DI! DI! And only DI!!!

  • @pedropc5824
    @pedropc5824 2 года назад

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