0
\$\begingroup\$

I want to create a list of horses and print the name of the Horses that are stored in the list. Although I got the answer correct as I desired, there's one thing that is puzzling me when I declare a list variable.

I declared it as:

public List<string> myFavouriteHorses;

and in an example I have seen it's declared like this:

public List<string> myFavouriteHorses = new List<string> ();

Despite this difference, both of them worked. What's the difference between these two approaches? Does it matter which one I use?

Here's the entire code which i tried:

using UnityEngine; 
using System.Collections.Generic;

public class LearningScript : MonoBehaviour {

    public List<string> myFavouriteHorses = new List<string> ();

    void Start(){
        myHorses ();
        }

    void Update(){
        if (Input.GetKeyDown (KeyCode.Return)) {
            Print ();

        }

    }

    void myHorses(){

        myFavouriteHorses.Add ("Grey Horse");
        myFavouriteHorses.Add ("Black Horse");
        myFavouriteHorses.Add ("Rudolph Horse");
        myFavouriteHorses.Add ("Biting Horse");
    }

    void Print(){
        Debug.Log ("This list has " + myFavouriteHorses.Count + " horses");

        Debug.Log ("The horse at index 1 is " + myFavouriteHorses [1]);
        Debug.Log ("The horse at index 2 is " + myFavouriteHorses [2]);
        Debug.Log ("The horse at index 3 is" + myFavouriteHorses [3]);

    }

}

Here's the output

Output

Why doesn't it make a difference whether or not I initialize the List with new before using it?

\$\endgroup\$
7
  • 2
    \$\begingroup\$ What is your question? \$\endgroup\$
    – jgallant
    Sep 2, 2016 at 11:38
  • \$\begingroup\$ public List<string> myFavouriteHorses = new List<string> (); VS public List<string> myFavouriteHorses; \$\endgroup\$
    – ThePercept
    Sep 2, 2016 at 11:43
  • 2
    \$\begingroup\$ This is not a game development related question. Voting to close. \$\endgroup\$
    – jgallant
    Sep 2, 2016 at 11:43
  • \$\begingroup\$ What is your question? \$\endgroup\$
    – Vaillancourt
    Sep 2, 2016 at 11:44
  • \$\begingroup\$ why do we use ' = new List <string> ' ? \$\endgroup\$
    – ThePercept
    Sep 2, 2016 at 11:44

1 Answer 1

2
\$\begingroup\$

Unity's editor automatically creates the new List () part when generating the game. That's how you can already populate it with data in the editor. Things assigned in the editor will be added to the gameobject even BEFORE Awake() and Start() so if you assign the variable somewhere in Awake() or Start() (or in a function called during one of them, such as myHorses()) it will override the assignment done in the editor.

\$\endgroup\$
5
  • \$\begingroup\$ What? No. You are populating the list in myHorses ();. You call myHorses (); in start ();, so when you come to access members of the list myFavouriteHorses it already has all the elements in it. \$\endgroup\$
    – James T
    Sep 2, 2016 at 11:44
  • \$\begingroup\$ Oops. I should edit that to be more clear. \$\endgroup\$ Sep 2, 2016 at 11:49
  • \$\begingroup\$ Are you sure inspector properties are assigned after Awake()? I could have sworn I've read this information in Awake without issues... \$\endgroup\$
    – DMGregory
    Sep 2, 2016 at 11:57
  • \$\begingroup\$ I'm actually not 100% certain, but if there's a place it would happen, it would be there. I'll go double-check. \$\endgroup\$ Sep 2, 2016 at 11:59
  • \$\begingroup\$ Never mind, I was wrong. Just tested it, and the inspector variable was already changed by the time Awake was called. Editing now. \$\endgroup\$ Sep 2, 2016 at 12:06

You must log in to answer this question.

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