1

Initializing four items into a List as follows, BUT it only initializes the first two items ... I really can't see what is wrong with this?

public List<SceneStore> lcRecordList = new List<SceneStore>
{
    new SceneStore { description ="Jill ", ID = 1, visited = false },
    new SceneStore { description = "Jack", ID = 2, visited = false},
    new SceneStore { description = "Joe", ID = 3, visited = false},
    new SceneStore { description = "Jenny", ID = 4, visited = false}
};
void NetTest()
{
    NetworkService lcMyNetworkService = new NetworkService();

    lcMyNetworkService.PutJsonList <SceneStore>(lcRecordList, "https://NewSimland.com/~todd/JSON", ReceiveAListOfRecords);
}

Took a screenshot

local variable lcRecordList

This adds four items just fine:

public List<SceneStore> lcRecordList;
void NetTest()
{
    NetworkService lcMyNetworkService = new NetworkService();
    lcRecordList = new List<SceneStore>
    {
        new SceneStore { description ="Jill ", ID = 1, visited = false },
        new SceneStore { description = "Jack", ID = 2, visited = false},
        new SceneStore { description = "Joe", ID = 3, visited = false},
        new SceneStore { description = "Jenny", ID = 4, visited = false}
    };
    lcMyNetworkService.PutJsonList <SceneStore>(lcRecordList, "https://NewSimland.com/~todd/JSON", ReceiveAListOfRecords);
}

So why is initialization of lcRecordList on declaration limited to the first two?

Took another screenshot of the local variable value:

Initialization in the method initialized all four items

4
  • Is the class a MonoBehaviour? If it is, then it means that the value that you see in the inspector will override the field initialization value.
    – PNarimani
    Oct 21, 2018 at 20:45
  • OP, you basically forgot the ".Add" !
    – Fattie
    Oct 21, 2018 at 21:28
  • How many items are in the list when viewed in the inspector? Does resetting the monobehaviour help? Right click in inspector and click Reset, this will clear the serialized fields so be careful not to lose anything important.
    – CaTs
    Oct 21, 2018 at 22:48
  • Ah OKIE DOKIE !! GOT IT!! The Mo Narimanu and CaTs! You are correct!! clicking Reset on the Inspector works!
    – Hui Teki
    Oct 21, 2018 at 23:17

2 Answers 2

1

As indicated by CaTS and Mo Narimani, the UNITY3D environment is initializing the values based on the first time the list was initialized because it found a Public class attribute (aka variable) in the MonoBehaviour. That was overriding the initialization in the script after more items were added in the script code.

SO the Answer is to "refresh" that by clicking on Reset in the Inspector after adding more items , when changing the initialization on declaration, that works!!

See screenshot here:

Unity3D sticks with the first initialization?

-1

try this instead:

   public List<SceneStore> lcRecordList = new List<SceneStore>
    {
      SceneStore store;
      store=  new SceneStore { description ="Jill ", ID = 1, visited = false },
      lcRecordLisr.Add(store);

        store=   new SceneStore { description = "Jack", ID = 2, visited = false},
        lcRecordLisr.Add(store);

        store=   new SceneStore { description = "Joe", ID = 3, visited = false},
        lcRecordLisr.Add(store);

       store=    new SceneStore { description = "Jenny", ID = 4, visited = false}
        lcRecordLisr.Add(store);

    };
2
  • you can just combine those in one, if you wish.
    – Fattie
    Oct 21, 2018 at 21:28
  • The problem is initialization on declaration actually initializes the first two but there are four items. Added further details above.
    – Hui Teki
    Oct 21, 2018 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.