0
\$\begingroup\$

I was trying to understand the difference of int and public int by checking how unity respond to it

using UnityEngine;
using UnityEngine.Assertions.Must;
using UnityEngine.SceneManagement;

public class Bird : MonoBehaviour

{
 int CurrentState = 1;
 int PreviousState = 1;

}
Void Update()
{(if CurrentState != PreviousState)
 transform.localScale = new Vector3(transform.localScale.x+1,transform.localScale.y+1,transform.localScale.z+1);
  }

Since the equality is false, the scale of object remail unchange and in my next step, I changed "int CurrentState = 1" to "public int CurrentState=0" and "int PreviousState = 1" to "Public int PreviousState = 1" . Isn't that this time, the equality is true as 1 !=0 but the scale of object remained the same. Once I deleted the word "public" on both currentstate and previousstate, the scale increase indefinitely. I thought the word "public" basically meant there no access restriction but why it looks like the "if" statement couldn't retrieve the info correctly?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

public variables are visible to Unity's serializer, so you can assign values to them in the Unity Editor's Inspector.

That means that the value in your field initializer code might be replaced by values saved in the Inspector before your if statement runs.

So: when working with public or serialized variables, always check the inspector to see if you left them set to a different value than you meant to.

\$\endgroup\$
2
  • \$\begingroup\$ Thank for your good suggestion but after I tried it, found that this is very strange. At first I put [SerializeField] public int PreviousState = 0; so in my inspector it does pop out a serializer to fill in the number. After that I deleted "[SerializeField]", the serializer still appear in inspector even after I saved the script. The serializer only gone if I delete the word "public" It looks like in unity, the word "public" is not just as an access modifier, there more function to it? \$\endgroup\$
    – chuackt
    Commented Nov 8, 2020 at 9:13
  • 1
    \$\begingroup\$ The very first line of my answer reads: "public variables are visible to Unity's serializer, so you can assign values to them in the Unity Editor's Inspector." So yes. Both public and [SerializeField] private/protected variables are subject to being assigned by the serializer. \$\endgroup\$
    – DMGregory
    Commented Nov 8, 2020 at 11:17
3
\$\begingroup\$

If you don't declare any access modifier (public, private or protected), the default is private, meaning it will not show in your inspector and will not be accessible from outside of the class.

Making the property [SerializeField] will make it show up in the inspector and you could edit it, but still, because it is private (by default) it will not be accessible outside of the class.

Making the property public will make it both show up in the inspector and other class could access this property from outside the class.

You might want to make your property public but make it not visible in the inspector, just add the [HideInInspector] attribute above the desired property, that way its accessible from outside the class, but not from the inspector.

You can read more about it here and here.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Considering how you are covering every other case, perhaps add public + [HideInInspector]? \$\endgroup\$
    – Weckar E.
    Commented Nov 9, 2020 at 7:12
  • \$\begingroup\$ @WeckarE.You are right, added, thank you :) \$\endgroup\$
    – Naor Hadar
    Commented Nov 9, 2020 at 9:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .