Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why is my variable 0?

Discussion in 'Getting Started' started by MikeTeavee, Jun 3, 2015.

  1. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    This is an example from the book I'm reading. The section that is giving me trouble is the part where float 'otherScale' is declared. Originally, there is no initialization, but I added 1.5f just for fun. Surprisingly, otherScale is equal to 0 inside Update() ...why?
    Tried googling but not even sure how to search for an answer!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class MoreScope : MonoBehaviour
    4. {
    5.         OtherScope other;
    6.         // Use this for initialization
    7.         void Start ()
    8.         {
    9.             other = (OtherScope)GameObject.FindObjectOfType (typeof(OtherScope));
    10.             Debug.Log (other.gameObject.name);
    11.         }
    12.         // Update is called once per frame
    13.         public float otherScale = 1.5f;
    14.         void Update ()
    15.         {
    16.             other.Size = otherScale;
    17.         }
    18. }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Public variables get their value from the inspector, not your code.

    --Eric
     
    NomadKing likes this.
  3. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    That mistake trips me up sometimes too, because it's easy to forget that the value of the variable may be different from the value typed in the code. To elaborate on his correct explanation, Unity automatically serializes all public variables, which means the value is set to what's typed in the Inspector. Initially this value will be what's typed in the code, but then if you change the code it won't change the Inspector again.
     
    konurhan and NomadKing like this.
  4. MikeTeavee

    MikeTeavee

    Joined:
    May 22, 2015
    Posts:
    194
    Thanks! So let's say I wanted the variable to have a initial value...I enter that in the Inspector rather than in the .cs file?
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Use the Reset() method to define any default values for your scripts. You can reset scripts using the gear icon in the inspector and hitting the Reset field.

    Any new scripts automatically run Reset() one time when added to the Inspector.
     
  6. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    You can specify an initial value when you write the variable the first time, and it will get used the first time. It just gets ignored any time after that; so type whatever you want for the new initial value (that will be used next time you add this script to a new object) but you also need to change the value in the Inspector on the existing object.
     
  7. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Just to add, you can also use HideInInspector if you want something to remain public but don't want it to appear in the editor.
     
  8. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    Actually, Unity will still serialize that variable. The difference is really subtle and obscure, but you want NonSerialized

    As long as we're bringing up these serializable attributes, lately I've been using private fields with SerializeField rather than public fields when I want to link objects in the Inspector. The purpose of the variable is clearer at glance.
     
    NomadKing likes this.