Search Unity

Objects in list turning to null

Discussion in 'Scripting' started by Lwyx, Jul 21, 2019.

  1. Lwyx

    Lwyx

    Joined:
    Sep 22, 2018
    Posts:
    3
    Hi everyone,

    I have encountered a strange issue while filling up a list of GameObjects, and I hope someone could clarify what is going on here.

    I go through the gameobjects of a public array, filled with prefabs assigned in the inspector, I instantiate and add them to a list. This is the code.

    Code (CSharp):
    1.  
    2. List<GameObject> listCharacter = new List<GameObject>();
    3.  
    4. .
    5. .
    6. .
    7.  
    8. for (int i = 0; i < CharacterManager.Instance.characters.Length; i++)
    9.             {
    10.                 int deltaIndex = i - currentCharacterIndex;
    11.  
    12.                 GameObject character = Instantiate(CharacterManager.Instance.characters[i], centerPoint, Quaternion.Euler(originalRotation.x, originalRotation.y, originalRotation.z));
    13.                 /*Character charData = character.GetComponent<Character>();
    14.                 //character.GetComponent<Rigidbody>().useGravity = false;
    15.                 if (character.GetComponent<PlayerBallController>()!=null)
    16.                 {
    17.                     Component ballController = character.GetComponent<PlayerBallController>();
    18.                     Destroy(ballController);
    19.                 }
    20.  
    21.                 //charData.characterSequenceNumber = i;
    22.                
    23.                 character.transform.localScale = originalScale;
    24.                 //character.transform.position = centerPoint + new Vector3(deltaIndex * characterSpace, 0, 0);
    25.  
    26.                 // Set color based on locking status
    27.                 //Renderer charRdr = character.GetComponentInChildren<Renderer>();
    28.  
    29.                 //if (charData.IsUnlocked)
    30.                 //    charRdr.material.SetColor("_Color", Color.white);
    31.                 //else
    32.                 //    charRdr.material.SetColor("_Color", lockColor);
    33.  
    34.                 // Set as child of this object
    35.                 character.transform.parent = transform;
    36.                 switch (scrollerStyle)
    37.                 {
    38.                     case ScrollerStyle.Line:
    39.                         character.transform.localPosition += new Vector3(deltaIndex * characterSpace, 0, 0);
    40.                         break;
    41.                     case ScrollerStyle.Circle:
    42.                         character.transform.localPosition = transform.InverseTransformPoint(centerPoint) + new Vector3(Mathf.Sin(-currentAngle + i * characterAngleSpace), 0, -Mathf.Cos(-currentAngle + i * characterAngleSpace)) * characterScrollerRadius;
    43.                         break;
    44.                     default:
    45.                         break;
    46.                 }
    47.                 // Set layer for camera culling
    48.                 character.gameObject.layer = LayerMask.NameToLayer("CharacterSelectionUI");
    49.                 //character.transform.GetChild(1).gameObject.layer = LayerMask.NameToLayer("CharacterSelectionUI");*/
    50.  
    51.                 listCharacter.Add(character);
    52.             }
    (As you can see, I commented everything trying to find the issue but it is still happening)

    The thing is that, whenever the GameObject "character" gets a new value, the previous value that it had and was added to the list "listCharacter" in the previous iterations turns to null, as if it was just like a reference to the object instead of a clone of it.

    So dividing the process in iterations, this is what is happening.

    After the first iteration everything works fine.
    The list is:
    list [0] = gameobject1

    After the second iteration, when the Instantiate line is executed (I debugged it), the list[0] gameobject "disappear" and turns to null. So I end up with this list:
    list[0] = null
    list[1] = gameobject2


    After the third iteration the same:
    list[0] = null
    list[1] = null
    list[2] = gameobject3


    After the fourth:
    list[0] = null
    list[1] = null
    list[2] = null
    list[3] = gameobject4


    etc etc.

    This is driving me crazy and I wasted too many hours trying to figure out what is going on :(
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,527
    Do the prefabs that you're instantiating have any scripts on it that call 'Destory(gameObject)' on themselves under some condition in its Awake/OnEnable messages?
     
    Lwyx likes this.
  3. Lwyx

    Lwyx

    Joined:
    Sep 22, 2018
    Posts:
    3
    Oh my god, you are absolutely right. One of the scripts in the prefabs had a singleton pattern. I can't believe I did not notice this.

    Thank you so much, really.