# Problem with List

**URL:** https://discussions.unity.com/t/problem-with-list/495352
**Category:** Scripting
**Tags:** Scripting
**Created:** [January 19, 2013, 3:16pm UTC](https://discussions.unity.com/t/problem-with-list/495352 "2013-01-19T15:16:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![SoulRyder89](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@SoulRyder89](https://discussions.unity.com/u/SoulRyder89)
#### Post date: [January 19, 2013, 3:16pm UTC](https://discussions.unity.com/t/problem-with-list/495352/1 "2013-01-19T15:16:33Z")

</div>

Hello,

When im in my game I can go to a menu, which is another scene. When I come back from this menu and load the game again I have the problem that the following list will be ruined:

```auto
public static var playerRows = new List.<GameObject>();

```

The Count of the list will still be the same, but the gameObjects inside the list will be gone and set to null.

Now my solution to this problem was to check if one of the gameobjects in the array is null, and if this is null it means that all the other gameobjects are null too, because the player went to another scene.  
So when it is null I will fill all the positions in the array with the corresponding gameobjects again.

```auto
if(playerRows[0].gameObject == null)
	{
		playerRows.Add(checkWinRow1);
		playerRows.Add(checkWinRow2);
		playerRows.Add(checkWinRow3);
		playerRows.Add(checkWinRow4);
		playerRows.Add(checkWinRow5);
		playerRows.Add(checkWinRow6);
		playerRows.Add(checkWinRow7);
		playerRows.Add(checkWinRow8);
		playerRows.Add(checkWinRow9);
		playerRows.Add(checkWinRow10);
		playerRows.Add(checkWinRow11);
		playerRows.Add(checkWinRow12);
	}

```

This works perfectly! But, I get the following error:

> [@](#):
>
> ArgumentOutOfRangeException: Argument is out of range.

Now I dont know what todo, cause the solution I made works perfectly, but having alot of errors in my console doesnt really give me the feeling I did a good job…

Is there a way I can fix this, or how can I do this in another way?

---

<div class="post-metadata">

### Author: ![gaweph](https://avatars.discourse-cdn.com/v4/letter/g/b782af/32.png) [@gaweph](https://discussions.unity.com/u/gaweph)
#### Post date: [January 19, 2013, 7:27pm UTC](https://discussions.unity.com/t/problem-with-list/495352/2 "2013-01-19T19:27:52Z")

</div>

PlayerRows is probably empty. So you cant get playerRows[0]. Check if player rows.Count is \> 0 before you try to get the first element.

---

<div class="post-metadata">

### Author: ![EddyEpic](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/eddyepic/32/111915_2.png) [@EddyEpic](https://discussions.unity.com/u/EddyEpic)
#### Post date: [January 19, 2013, 10:45pm UTC](https://discussions.unity.com/t/problem-with-list/495352/3 "2013-01-19T22:45:34Z")

</div>

To make the system “safer” from those kinds of errors, you could simply re-initialize everything as soon as you load a new level. Clear the list, and add the items to it again. This way, you don’t have to care if the objects got nulled out, because you are actively taking care of it yourself.

---

<div class="post-metadata">

### Author: ![SoulRyder89](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@SoulRyder89](https://discussions.unity.com/u/SoulRyder89)
#### Post date: [January 21, 2013, 5:02pm UTC](https://discussions.unity.com/t/problem-with-list/495352/4 "2013-01-21T17:02:06Z")

</div>

Thanks for the information!!  
I fixed it by always nulling the object at function Start() and then reassigning the GameObject List value to it.

```auto
public static var playerRows = new List.<GameObject>();

function Start()
{
	playerRows = null;
	playerRows = new List.<GameObject>();
	playerRows.Add(checkWinRow1);
	playerRows.Add(checkWinRow2);
	playerRows.Add(checkWinRow3);
	playerRows.Add(checkWinRow4);
	playerRows.Add(checkWinRow5);
	playerRows.Add(checkWinRow6);
	playerRows.Add(checkWinRow7);
	playerRows.Add(checkWinRow8);
	playerRows.Add(checkWinRow9);
	playerRows.Add(checkWinRow10);
	playerRows.Add(checkWinRow11);
	playerRows.Add(checkWinRow12);
}

```

As you see I also assign the list before any function is running, because otherwise unity will give me “is not member of” errors. But then I redo everything when the Start function is running.

---

<div class="post-metadata">

### Author: ![GaVaR](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/gavar/32/477980_2.png) [@GaVaR](https://discussions.unity.com/u/GaVaR)
#### Post date: [January 21, 2013, 6:16pm UTC](https://discussions.unity.com/t/problem-with-list/495352/5 "2013-01-21T18:16:34Z")

</div>

Better move this to Awake.  
Static variables are not serialized between game sessions. So, whenever the scene starts

```auto
public static var playerRows = new List.<GameObject>();

```

is executed, so you get error that argument is out of range.
