so for the life of me I cant figure out why this particular function is crashing my game public void DividePlanets () { Debug.Log ("sortPLanets called"); myPlanets.Clear (); enemyPlanets.Clear (); for (int i = 0; i < planets.Count; i++) { if (planets.isUCS == MyFactionToBool ()) { myPlanets.Add (planets); } else { enemyPlanets.Add (planets); } } } planets is a list of the planets in game, this particular list is just a reference to the actual list thats kept on another script that i grab a reference to in start. both myPlanets and enemyPlanets are local lists i use to temporarily keep track of which planets are under the current players control or thier opponents control. the MyFactionToBool() just returns a bool that i use for keeping track of the current faction but here it is anyways just in case. public bool MyFactionToBool () { if (faction == Faction.UCS) { return true; } else { return false; } }
Just a guess but when you call myPlanets.Clear (); enemyPlanets.Clear (); are you sure no one else try to access the data from one of them And also make sure to clone the lists and dont just point on the real i added some code Code (CSharp): List<Vector3> listA = new List<Vector3>(); List<Vector3> listB = new List<Vector3>(); void Start () { //works listA.Add (new Vector3 (1, 1, 1)); for (int i = 0; i < listA.Count; i ++) { listB.Add (listA[i]); } listB.Clear (); Debug.Log (listA [0]); listA.Clear (); //we clear the same list listA.Add (new Vector3 (1, 1, 1)); listB = listA; listB.Clear (); Debug.Log (listA [0]); // dont use this it will crash unity // listA.Add (new Vector3 (1, 1, 1)); // listB = listA; // for (int i = 0; i < listA.Count; i ++) // { // listB.Add (listA[i]); // } }
Code (csharp): public void DividePlanets () { Debug.Log ("sortPLanets called"); myPlanets.Clear (); enemyPlanets.Clear (); for (int i = 0; i < planets.Count; i++) { if (planets[i].isUCS == MyFactionToBool ()) { myPlanets.Add (planets[i]); } else { enemyPlanets.Add (planets[i]); } } }
Thanks for the reply. So after fooling around with my function some I managed to figure out it was these two lines that were causing the crash: Code (CSharp): myPlanets.Add (planets[i]); and: Code (CSharp): enemyPlanets.Add (planets[i]); you mentioned making sure I'm cloning the original list and not just referencing it, I'm not entirely sure I know what you mean (sorry still really new to scripting so I don't have a very strong grasp of the lingo). here is the actual assignment in start: Code (CSharp): planets = gameControllerScript.planetDatabase; I'm not sure if this is the cloning you are talking about or not. I had always thought when you assign a named variable another named variable you were actually just creating a reference to it.
For a long time I couldn't figure out what you had changed, so I went back and looked at my OP really closely and realized that when I just copy and pasted my code into the post it left out using the i for the index (also that seems to be why half my post is italicized apparently, lol) so now it looks like I'm trying to add the whole list of planets to myPlanets and enemyPlanets. I originally was using i for the index of planets but this was my first post so I couldn't figure out how to display what I had written as code. Thanks for the swift reply anyways!!