Search Unity

Please Help!

Discussion in 'Scripting' started by ascendedraven, Jul 3, 2015.

  1. ascendedraven

    ascendedraven

    Joined:
    Jul 3, 2015
    Posts:
    3
    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;
    }
    }
     
  2. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    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):
    1.  
    2.     List<Vector3> listA = new List<Vector3>();
    3.     List<Vector3> listB = new List<Vector3>();
    4.  
    5.     void Start ()
    6.     {
    7.         //works
    8.         listA.Add (new Vector3 (1, 1, 1));
    9.         for (int i = 0; i < listA.Count; i ++)
    10.         {
    11.             listB.Add (listA[i]);
    12.         }
    13.         listB.Clear ();
    14.         Debug.Log (listA [0]);
    15.         listA.Clear ();
    16.  
    17.  
    18.         //we clear the same list
    19.         listA.Add (new Vector3 (1, 1, 1));
    20.         listB = listA;
    21.         listB.Clear ();
    22.         Debug.Log (listA [0]);
    23.  
    24.         //        dont use this it will crash unity
    25.         //        listA.Add (new Vector3 (1, 1, 1));
    26.         //        listB = listA;
    27.         //        for (int i = 0; i < listA.Count; i ++)
    28.         //        {
    29.         //            listB.Add (listA[i]);
    30.         //        }
    31.     }
    32.  
     
    Last edited: Jul 3, 2015
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Code (csharp):
    1.  
    2.  
    3. public void DividePlanets ()
    4. {
    5. Debug.Log ("sortPLanets called");
    6. myPlanets.Clear ();
    7. enemyPlanets.Clear ();
    8.  
    9. for (int i = 0; i < planets.Count; i++)
    10. {
    11. if (planets[i].isUCS == MyFactionToBool ())
    12. {
    13. myPlanets.Add (planets[i]);
    14.  
    15. }
    16. else
    17. {
    18. enemyPlanets.Add (planets[i]);
    19.  
    20. }
    21. }
    22. }
    23.  
    24.  
     
  4. ascendedraven

    ascendedraven

    Joined:
    Jul 3, 2015
    Posts:
    3
    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):
    1.  
    2. myPlanets.Add (planets[i]);
    3.  
    and:
    Code (CSharp):
    1.  
    2. enemyPlanets.Add (planets[i]);
    3.  
    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):
    1.  
    2. planets = gameControllerScript.planetDatabase;
    3.  
    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.
     
  5. ascendedraven

    ascendedraven

    Joined:
    Jul 3, 2015
    Posts:
    3
    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!!