- Home /
Difference between [SerializeField] and public member
I can't able to understand difference between public variable or Serialize variable.
[SerializeField] Text connectionText;
or
public Text connectionText;
Though actual result of both line is same. You are seeing variable on Unity Inspector. What is actual difference?
Answer by Dinosaurs · Nov 19, 2015 at 04:34 PM
The difference is that in the first example, connectionText is still a private variable, that is not able to be accessed from other scripts.
@Dinosaurs, Thanks for your reply. Can we change value of serialize variable through inspector?and got affected in actual coding as like public variable?
Yeah, the field will just not be accessible in code.
The opposite process would be
[HideInInspector]
public int myVar;
where the variable is public but it won't show in inspector. There will be cases where you would need that, I have yet to find one. But I have seen it on some Unity assets.
Answer by Dave-Carlile · Nov 19, 2015 at 04:36 PM
This allows you to follow best practices for object design (not exposing internal object state to other objects so they can't muck with it), while at the same time allowing a designer to initialize that internal state at design time.