Search Unity

Beginner coding question

Discussion in 'Getting Started' started by joeolechnicki, Mar 14, 2018.

  1. joeolechnicki

    joeolechnicki

    Joined:
    Mar 13, 2018
    Posts:
    2
    I am an absolute beginner in both C# and Unity, and the book I am suing to learn basic C# has me going through a simple math script and showing me how it works in the engine.

    using UnityEngine;
    using System.Collections;

    public class LearningScript : MonoBehaviour {

    public int myNumber = 9;

    // Use this for initialization
    void Start () {

    Debug.Log(2 + 9);

    Debug.Log(11 + myNumber);

    }

    // Update is called once per frame
    void Update () {

    }
    }

    So myNumber is 9, and the Unity console displays this:
    I understand this much - it's just solving math problems.

    But when I change myNumber to 19, and (2 + 9) to (2 +19) I get this:

    Why is it not registering that myNumber was changed to 19?
    Also, why is it showing me the message twice?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Welcome to Unity. Hope ya have some fun!

    It's possible that you have the script on 2 game objects in the scene (or even twice on the same game obect).

    As for your number there and the discrepancy, if you modified the variable in the inspector, that inspector value takes precedence. You can update the value in the inspector, or you can use the cog wheel 'settings' icon on the script, in the inspector, to reset it. The inspector is much easier, generally speaking, but it doesn't hurt to know both :)
     
  3. joeolechnicki

    joeolechnicki

    Joined:
    Mar 13, 2018
    Posts:
    2
    Thanks, hope to get some useful info from folks here. How do I edit it from inside the inspector box? I thought the only way to edit the script was through monodevelop.

    side note: just check out your profile, and we have the same birthday.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    lol, cool :)

    As far as the inspector goes, I meant the public variable myNumber. That would just be when you select the game object to which the script is attached and look at the variable.

    The Debug Log with the 2 actual numbers together (not a variable) must be edited in the code, of course :)
     
  5. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    This is because "myNumber" is public. So Unity has saved your object in the scene with the value of "9" (you should be able to see that in the inspector). When the scene is loaded, unity gives it the saved value of 9, which overwrites your 19.