Search Unity

Array Problem

Discussion in 'Scripting' started by Volturna, Sep 29, 2020.

  1. Volturna

    Volturna

    Joined:
    Jul 13, 2014
    Posts:
    28
    Will try my best to explain my problem...

    I have several objects with the same script attached called "NODE". I created an array in that script, ike this:


    public class NODE : MonoBehaviour {

    public int[] FaceSkinTileX = new int[6];

    }


    This array will handle 6 diferente values that will change from object to object. so i need every object to have their own array.

    But every time i try to change those values within a specific object, every object will get same values for the array.

    I'm changing those values by doing this:

    Obj.GetComponent<NODE>().FaceSkinTileX[i] = something;


    What am i doing wrong?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    It has more to do with how you create and initialize the arrays than how you change the values. If you're not intentionally creating/instantiating those arrays anywhere in code, are you perhaps instantiating the object that holds the array? If so, then the clone is probably pointing to the same array in memory when it's created, and you should probably initialize them in Awake().
     
    Bunny83 likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    I'm going to guess that this code is probably inside a for loop, and therefore is applying to every element in the array.
     
  4. Volturna

    Volturna

    Joined:
    Jul 13, 2014
    Posts:
    28
    Correct. "i" will go from 0 to 5
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Yep, so your code is working exactly as written. That's what a for loop does: "For every value from 0 to 5, do this thing". So it will happen to every element in the array. If you just want to access a single element of the array, don't use a for loop. Just access it by index directly. For example, to set the third element in the array (index 2):
    Code (CSharp):
    1. myArray[2] = something;
     
  6. Volturna

    Volturna

    Joined:
    Jul 13, 2014
    Posts:
    28
    Maybe i explain myself in the wrong way. The problem is that if i change index 2 to be equal to "something" then every array in my object collection will change to that value at index 2!
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    It sounds like you might have multiple references to the same array, instead of different arrays.

    Each array needs to be created with
    new int[size]
    or Array.Copy. If you assign the array simply like
    myArray = someOtherArray
    , then you haven't created a new array, you've just created a new reference to the same array.
     
    Joe-Censored likes this.
  8. Volturna

    Volturna

    Joined:
    Jul 13, 2014
    Posts:
    28
    I just rebuilt my code and now its working. I had an update to the last array that was passing its values to all the others... so dumb...

    anyway, thank you both