#4 | PROPERTIES & GET/SET METHODS 🎮 | Unity For Beginners | Unity Tutorial

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

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

  • @Dani_Krossing
    @Dani_Krossing  2 года назад +48

    Hi everyone! Yes I had to re-create this video since there was a mistake in my example, which might have confused a lot of people since I say one thing, but show another hehe. Also, a friendly commenter named Matthias made me aware that I had misunderstood how "auto-properties" functioned internally, so here is a corrected version since I don't wish for my lessons to be misleading. 😊 So thank you Matthias.

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

      No worries, you're welcome, don't have to mention it. That you thought of additionally telling people that one can use the same field for multiple properties restructuring the output for different usecases is a pretty cool. Something that's not mentioned often. Well done. Cheers.

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

      Hi, is array work with get/set method?
      like this one public int[] Playerhealth { get; set; }

    • @Sam4Progress
      @Sam4Progress 7 месяцев назад

      Something that others may have asked, and I''m surprised you didn't add, is what DOES the clamping look like? Is it as simple as PlayerHealth

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

    god bless you elon musk i just wanted to let you know that your c# videos help me substantially and you are signifcantly better then the professor teaching the course i am currently in

  • @145Jamie
    @145Jamie 11 месяцев назад +2

    This is the BEST explanation of 'Properties' I have ever come across.

  • @rrydaa2316
    @rrydaa2316 2 года назад +21

    These are actually great videos that help me understand what I’m doing instead of just doing it

  • @KillAllCommies2
    @KillAllCommies2 Месяц назад +1

    It is rare in game development to see a tutorial that actually explains what is being done and why it is being done instead of just writing billions of lines of code in a second.

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

    Thank you so much for explaining clearly and succinctly how and why to do this! So many tutorials tell you how but never explain exactly why and when to use something.

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

    In Visual Studio you can type "prop" and hit tab twice and it will fill out the property for you. You can then hit tab to scroll through the name and datatype of the property. If you do "propg" instead it will create a property with a private setter which is my preference for creating immutable data structures. This functionality is called a "snippet", you can make your own too.

  • @VictorQiu-ol5ms
    @VictorQiu-ol5ms 5 месяцев назад +2

    you teach a lot better than those paid teachers on udemy, you saved my day !

    • @sladethegreat1194
      @sladethegreat1194 4 месяца назад

      You’re here from the rpg course aren’t you?

  • @shahroznaeem
    @shahroznaeem Год назад +1

    You can type out "prop" and hit tab to use intellisense to create an auto property quickly. Default behavior creates a public integer property.

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

    Dude, thanks to you, now I feel like I can make any website, I’m goin go to new languages already, thank you very much sr

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

    I think I've found the perfect tutorial. Thank you very much and do not stop doing it, please.

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

    Thank u so much for everything, I am just starting up and follow all ur step, and it was really beginner friendly! Absolutely splendid!

  • @FoxeyWind
    @FoxeyWind 5 месяцев назад

    Just started watching this series, very good you tech me ehat to do instead of just me copying code.

  • @McPatoo
    @McPatoo Год назад +2

    Thanks a lot sir, we need more ppl like you. i like ur way to explain every step :DDDDD

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

    The C# syntax for get and set methods (properties) is something I will have to get used to. I'm used to the way that Java creates get and set methods.

  • @SaitamaMixed
    @SaitamaMixed Месяц назад

    Earned a new subscriber, master. Looking farward to gain more knowledge

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

    Oh man i was so confused but it was something i was doing in my code already, in a way.
    I did have getters/setters but i implemented them through seperate functions. Guess i can replace those with properties now that i know its pretty much same thing, except likely more clear since its all in one place. Thanks a lot!

    • @Dani_Krossing
      @Dani_Krossing  Год назад +1

      It's important to note with getters and setters, that they aren't replacement for functions/methods. 🙂
      A function is meant to have a specific purpose, such as calculating something based on user input, or flipping the player character... Basically to perform complex operations, calculations, or tasks that go beyond simple retrieving or modifying data.
      Where as a getter or setter, is more about allowing you to control how fields are read and modified.

    • @Bobster536
      @Bobster536 Год назад +1

      @@Dani_Krossing noted, thanks! Will make sure to visit some of your other tutorials aswell.

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

    By the way, it is possible to expose properties to the Unity inspector by adding the tag [field: SerializeField] above the property.

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

    The quick code change at 9:19 made me think I was going crazy for a second.

  • @JK-pp2xl
    @JK-pp2xl 2 месяца назад

    Hi Dani, I am trying to learn Unity. I have coded in AGK and I would use what is known as a type. For instance, I would create a type Playertype; to store position, exp, health, maxhealth, buffs, basemovement, equipped items, etc. I call this back from player.health, player.exp, player.maxhealth, etc.
    I thought that was what you were getting at. Instead.. I am not real sure what we could use here. Your health % only worked because you have it set at 100. If you had 500 HP then your code would not return the percent. All you were doing was concatenating % to the value? So, if you had 500 HP it would say 500%? How could I store my characters base movement speed? Or their buffs to base movement speed? Or in your example buffs to actually increase their max health? Or actually show their health as a % and not just adding % at the end. Thank you.

  • @ikonhero
    @ikonhero 27 дней назад

    Quick question: why was the variable in the Field called _playerhealth = 100 set to private, but the property Playerhealth set to public?

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

    Thanks bro for this series. 😀🙌🏻

  • @vcuclutchyop5339
    @vcuclutchyop5339 9 месяцев назад

    Loving this series but i have a question about auto property ? The field that will be created in that so what would be the scope of that field public or private ?

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

      That’s a really good question. 🙂 I’m assuming the field is private, seeing that if it is public, then it defeats the purpose of using properties.

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

      @@Dani_Krossing Thanks alot you're a absolute banger

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

    love the inheritance joke

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

    Really helpful video, thank you!

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

    You did a great tutorial series man !
    but what makes me using get set, if changing PlayerHealth from another script it will change _playerHealth too
    and you said in the previous video this is not Good thing
    and why do i need to make a get while i just can do a function with return value to make it read only ?
    and why do i need to make a set while i can make a function to set that value, and do all kinds of operations i want before setting it ?

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

    i finally understand it

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

    Top quality!

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

    what is the "keyboard" I do not remember... but what is the keyboard combination to create properties with just simple keyboard ( Imeant for example , press "tab" + T and create a propertie full with get; and set.

  • @Hemecan.
    @Hemecan. 2 года назад +6

    Guess what I just realized : You look just like Elon Musk man! :)

  • @irtezamasud1079
    @irtezamasud1079 2 месяца назад +1

    Elon musk little brother is teaching code, what else you want??
    Btw, nice explanation!

    • @jimmoore3923
      @jimmoore3923 11 дней назад +1

      he should go and make a withdrawal from a certain bank acct 🤣

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

    i'm confuse, you said that if I have a property PlayerHealth{get; set} , it will auto create a field of _playerHealth, but i tried and can't access the field inside the class. Why?

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

      Because the field is private 🙂 you have to use the property to access it.

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

    great video

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

    does "playerhealth = value;" mean the value of the playerhealth?
    so the value would be 100?

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

      Yes because we assigned the player health inside our Field, in this example here it would be 100. 🙂 Later on when we get into creating objects and using constructors, we won't assign a value directly inside our class, but instead will assign it as we create objects.

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

    Good video, but was too distracted by your low cut shirt.

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

    How to make array of properties?

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

    hey man im currently making a tower defense game with a slow tower, but every time i spawn a second slow tower the variable on my first tower resets to the newly instantiated tower.
    Does this property method prevent that? or should i look else where. i still don't really get the concept :(

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

      No properties doesn't really solve that issue. 😕 It's just a "better" way of accessing/changing your class fields (what I assume you refer to as variable).
      If you just create a field inside your class, and then attach the script to your gameobject, then it should just be working for that one gameobject.
      Based on your issue as you describe it, it sounds to me that you are using a variable that is global, like a static variable, rather than a normal one. Since this would be a reason for why this would happen.

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

      ​@@Dani_Krossingso yes i do have a script that uses public static instance if im correct this is a singleton(does this count as global?). and this script would spawn a the towers. but the class fields is on a different script though it is on the same Tower gameobject. could that be the culprit?

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

      @@lokosstratos7192 A static field or method will only have one instance of itself, meaning if you change it at some point, it will change everywhere. 🙂 So yes that is my guess for what is causing your error.
      It's a general rule of thumb to never use static unless you have a good reason to do so. One place it can be used is for example if you need to create a manager for something, that only need to have ONE instance of itself.
      So no matter what script changes the static field, or reference to it inside itself, will always get changed if you change the static field.

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

      @@Dani_Krossing Thanks man this has been confusing me for days XD

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

    Hi Dani, I followed the tutorial step by step but keep getting this error message in the console that won't let me test the code: Assets\Scripts\Test.cs(48,9): error CS0104: 'Debug' is an ambiguous reference between 'UnityEngine.Debug' and 'System.Diagnostics.Debug'. How can I test if the code works without using the Debug.Log?. Thank you!.

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

      It appears that Unity can't figure out which "Debug" you are trying to use... Meaning you most likely have more than one namespace at the top of your file, which has the Debug reference in them... Did you include any extra namespaces at the top? Like "using System.Diagnostics;" ?

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

      @@Dani_Krossing You are absolutely correct sir, there was a "using System.Diagnostics;" namespace at the top, I don' exactly know why, but there it was. It works fine now!, thank you!.

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

      @@facundobologna2622 Glad you got it solved. 🙂 Be careful when adding namespaces that Unity doesn't provide, since it can lead to those errors.

  • @VitasIndrelė
    @VitasIndrelė 6 месяцев назад

    this playlist has the most explanation in it and i still dont understand what is happening here and why. All thoes fancy words are killing me

    • @Dani_Krossing
      @Dani_Krossing  6 месяцев назад

      It's VERY normal to not remember words like "properties", "fields", "methods", etc, in the beginning. It's part of learning how to program. 🙂
      What helps, is creating a comment above your code with their names, every time you use them. And if you don't remember, then look it up again. 🙂
      I know it sounds cliche, but the only way to "fail", is to give up. Since it's just one of those things that require "time", until it sticks eventually.
      We ALL had to go through this hehe.

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

    Hello everyone, I'm at 8:23 of the video when he's showing us how to display the players health on the console but for whatever reason it is not displaying it. I don't receive any errors and from what I can tell the code is the same as the video. Can anyone point out what I'm missing here. Thank you.
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Test : MonoBehaviour
    {

    private int _playerHealth = 100;
    public int PlayerHealth
    {
    get
    {
    return _playerHealth;
    }
    set
    {
    _playerHealth = value;
    }
    }
    public string DisplayHealthPercentage
    {
    get
    {
    string health = _playerHealth.ToString() + "%";
    return health;
    }
    }
    private void Start()
    {
    Debug.Log(DisplayHealthPercentage);
    }
    }

    • @Dani_Krossing
      @Dani_Krossing  Год назад +1

      I can’t see any errors at a quick glance. Are you sure the script is attached to a game object?

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

      @@Dani_Krossing Hey Dani, thank you for responding but yes. Everything worked fine up to this point. I was following your code and went to unity when you did to see the changes and everything was working fine up to this point. I don't know what other information I can provide. I want to really understand your tutorials before moving on the rest of your videos.

    • @Dani_Krossing
      @Dani_Krossing  Год назад +1

      Best advice I can give you, is that the error is with Unity, and not the code, since I can’t find any errors on my 2nd look through. So check anything Unity related, that could have gone wrong. 🙂 Things like making sure you saved your document, that the script is attached to a game object, and that it is the correct script in case you accidentally created multiple at one point while following the video… etc…

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

      @@Dani_Krossing Okay thank you. I'll double check and move forward. Appreciate you responding.

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

      @@Dani_Krossing So I continued with the tutorials and I'm on the 5th video. Something must be wrong with unity some kind of setting because nothing is showing up on the console. I'm at the 9:00 minute mark where it shows the players health as 90. I wish I could post a screenshot of unity but the document is saved, the script is attached to the game object and I only have one script as you do in the video. lol idk what's going on.

  • @garrettblackard2288
    @garrettblackard2288 7 месяцев назад

    Elon musks twin brother is teaching us code

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

    Its funny. When people teach programming, they never teach this side of programming.

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

    Your game doesn't even have health boosters??
    Your game sucks, I demand health boosters 🤣🤣🤣
    In Javascript and PHP this would be a class inside a class (sort-of) 😅

  • @Danjoyas
    @Danjoyas 2 месяца назад

    Jewfjewf9jew

  • @fisher_dittburner
    @fisher_dittburner Год назад +2

    the most important part of this to remember is "jewfjew9jew"