Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Why use Serializefield over just making public var

Discussion in 'Scripting' started by GigglyTwigg, Feb 17, 2021.

  1. GigglyTwigg

    GigglyTwigg

    Joined:
    Sep 7, 2020
    Posts:
    3
    Code (CSharp):
    1.  
    2.  
    3. [Serializefiled] int moveSpeed;
    4.  
    5. vs
    6.  
    7. public int moveSpeed;
    8.  
    9.  
     
  2. GigglyTwigg

    GigglyTwigg

    Joined:
    Sep 7, 2020
    Posts:
    3
    Hi so im just curious if this is a preference thing or is there a difference between the two from my limited exp and knowledge it does the same thing?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    JamesArndt, Schneider21 and Vryken like this.
  4. GigglyTwigg

    GigglyTwigg

    Joined:
    Sep 7, 2020
    Posts:
    3
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    It took me a while to come to understand the importance of enforcing access control on scripts. Out of the gate, I liked how easy it was to change values directly by making them public. I could work faster and write less code.

    One of the problems this creates is allowing you to make code that is tightly coupled. Meaning all my scripts would tend to be interconnected in a way that wasn't easy to maintain or update. Setting proper access control forces you to think of things as separate systems and will save you a lot of trouble in the long run.