Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

A single line of code (List.Add) crashes Unity.

Discussion in 'Scripting' started by jju1, Jun 22, 2016.

  1. jju1

    jju1

    Joined:
    Jun 22, 2016
    Posts:
    4
    The line of code on line 17 crashes Unity when I attempt to run it. When it is removed from the code everything runs dandy. The following code is supposed to take the gameObject "GUI" and make it and all of its children and their children etc invisible, but I am clueless as to why this single line of code keeps causing it to crash. Please help, thanks in advance.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5.  
    6. public class GuiHider : MonoBehaviour {
    7.  
    8.     public void ChangeGui(GameObject GUI, bool Set){
    9.         List<GameObject> Parents = new List<GameObject> ();
    10.         List<Component> Transforms = new List<Component> ();
    11.         Parents.Add (GUI);
    12.         while(Parents.Count > 0){
    13.             List<GameObject> Original = Parents;
    14.             for(int i = 0; i < Original.Count; i++){
    15.                 Transforms = Original [i].GetComponentsInChildren (typeof(Transform),true).ToList();
    16.                 for(int j = 0; j < Transforms.Count; j++){
    17.                     Parents.Add (Transforms[j].gameObject);
    18.                 }
    19.             }
    20.             for(int i = 0; i < Original.Count; i++){
    21.                 for(int j = 0; j < Parents.Count; j++){
    22.                     if(Original[i] == Parents[j]){
    23.                         if(Parents[j].GetComponent<SpriteRenderer>() != null){
    24.                             Parents [j].GetComponent<SpriteRenderer> ().enabled = Set;
    25.                         }
    26.                         if(Parents[j].GetComponent<TextMesh>() != null){
    27.                             Parents [j].GetComponent<MeshRenderer> ().enabled = Set;
    28.                         }
    29.                         Parents.RemoveAt(j);
    30.                         j--;
    31.                     }
    32.                 }
    33.             }
    34.         }
    35.     }
    36.  
    37. }
    38.  
     
  2. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Code (CSharp):
    1. List<GameObject> Original = Parents;
    It looks like you're expecting that to create a completely separate copy of the list, but lists (objects) are references. All you're doing is making Original point to the same list as Parents.
     
    jju1 likes this.
  3. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Edit: This was a reply to a now deleted message :)

    You can do this, but not quite the way you're doing it.

    You either have to copy the elements into the new list using a loop, or there's a list constructor that lets you pass in another list and it will copy the references for you.

    Code (CSharp):
    1. var original = new List<GameObject>(Parents);
     
    jju1 likes this.
  4. jju1

    jju1

    Joined:
    Jun 22, 2016
    Posts:
    4
    Thanks, but following up on that. How would I create a completely separate copy of the list?
     
  5. jju1

    jju1

    Joined:
    Jun 22, 2016
    Posts:
    4
    Thank you so much!!
     
  6. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    See previous reply :p
     
  7. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Haha, there seems to be a temporal anomaly going on.