Renaming a Field Without Losing the Value in the Inspector (Unity Tip)

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.ค. 2024
  • In this Unity quick tip we're going to look at how we can rename fields in a script without losing the values we have set in the inspector.
    If you prefer written instructions you can find a link to the text version here - www.ketra-games.com/2021/02/r...
    00:00 - Introduction
    00:20 - Demonstration of how field values are lost when renaming
    01:41 - Renaming fields safely using the FormerlySerializedAs Attribute

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

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

    Thank you for the concise lesson. I wish I had sought out this video sooner!
    I really like your videos, showing the incorrect and then correct way; explaining why. It's like I'm back in college :)

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

      Great to hear 😊

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

    Thank you so much, this is gold

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

      Great to hear, thanks 😊

  • @InsidiousRat
    @InsidiousRat 11 หลายเดือนก่อน +1

    The biggest question is whether or not you can delete the attribute later. My experience shows that sometimes you will lose values if you do that. I wish you've touched this in your video

  • @Bob-ts2tu
    @Bob-ts2tu ปีที่แล้ว

    many thanks, i am studying a new to me project with i dont know how many occurances of a script, yet but knew i needed to give a property a more specific name to distinguish it from others in different scripts with the same name, so thought i'd have to do something like enter a new property & then plough through all the prefabs, duplicating the value and when done delete the old property, a task i didnt relish, but this has saved me lots of time :)

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

      Glad you found it useful 😊

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

    Your videos are gems

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

    omg this is gold

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

    Very handy. I expect we can delete the attribute later once Unity associated the value with the new variable?

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

      Hi, yes, the next time you go into the script you can delete them 😊

    • @InsidiousRat
      @InsidiousRat 11 หลายเดือนก่อน +1

      @@KetraGames I believed you and now I lost hundreds of values...

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

      @@InsidiousRat You should use git, it will help you in the future in situations like this. Btw the op might've thought that it's safe to delete the attribute. because it looks like it is, only after a scene reload the references will be lost.

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

    This is very useful in a lot of ways (the seriallization, the calling later rotation etc) but I don't get why (after you rename the rotationSpeed to Amount) that it doesn't spin until you change the number. Can you please explain that. I'm only just beginning w/ Unity.

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

    i've tried it and it worked. but then I removed it after I renamed it then add the SerializeField again and it lost the reference. looks like I should keep it? cus it's sucks to have long attribute like that for each variable renamed

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

    Very cool! Question: After completing these steps, are you then able to remove the FormerlySerializedAs attribute safely?

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

      Thanks 😊
      Yes, you can safely take it out later. Sorry, I should have included that in the video

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

      @@KetraGames I tried removing that attribute for the same scene is fine but in other scene it is resetting

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

      @@KetraGames No, you can't. After reloading a scene you will loose all your references. Try reopening your Unity project and see how all your references are lost.

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

    Is there a way to do this with class names?

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

      Hi, there isn't an Attribute that I'm aware of, but if you rename the script in Unity and then rename the class it should all still work 😊

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

    Spoiler. Add [FormerlySerializedAs("...previous name...")]. See? That was quicker than watching a video!

    • @KetraGames
      @KetraGames  3 ปีที่แล้ว

      😂 Thanks for your comment