Search Unity

Cloned arrays and memory leaks

Discussion in 'Scripting' started by eco_bach, Sep 8, 2019.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Hi
    Trying to anticipate possible issues with a cloned array.


    I've defined 2 public accessors like this

    Code (CSharp):
    1.     public GameObject[] Icon_Prefabs; //size gets set in inspector! drag prefabs in there!
    2.     public GameObject[] clonedIcons;
    And then in my Start method I have


    Code (CSharp):
    1. void Start(){
    2. for (int i = 0; i < Icon_Prefabs.Length; i++)
    3.         {
    4.          
    5.              clonedIcons[i] = Icon_Prefabs[i];
    6.         }
    7.  
    8. }
    And lastly in my Update I am constantly Instantiating GameObjects from the clonedIcons Array.
    These GameObjects Destroy themsleves after a random interval.

    Will I encounter memory leaks or other issues using this logic(with a cloned array)?
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    clonedIcons is just going to be a reference to the objects in the Icon_Prefabs array. I'm not sure why you are using it. Although, if you are creating a destorying objects every frame, look up and study object pooling.