Search Unity

Array Length being set to zero

Discussion in 'Scripting' started by edufireheart, Jun 30, 2018.

  1. edufireheart

    edufireheart

    Joined:
    Aug 6, 2017
    Posts:
    11
    Hi, I have a simple slice of code where I define two arrays, which I set in the Editor, both having Length equal to 2. However, when I enter play mode, both array lengths are being set to zero inadvertidely and I get out of bounds errors.
    I tried erasing the script and creating it again and restarting Unity but it continues to happen. No other script is trying to use this arrays, I've just created them to use only inside this script here.
    It seems to me like a bug.
    Does anyone have a clue about what's happenning?

    [SerializeField] private GameObject[] buttons;
    [SerializeField] private Vector2[] buttonsPositionsToSet;
    [SerializeField] private float minimumDistanceToChangeSelection = 0.2f;
     

    Attached Files:

  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I just tried your script (had to comment out all lines using gameControl) and it didn't reset those elements to zero.

    Try creating a blank new scene. Add an empty game object with your script (commenting out the same lines I did). Set the counts to 2 in the Editor. You should find they do not reset to zero on running.

    Now you can start adding stuff from your real scene into this test scene. Find out at what point it breaks.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Are you sure you don't have multiple copies of the script in your scene? Search for the script in the scene or, for your debug, change it to...

    Debug.Log("Length: " + buttonsPositionsToSet.Length. gameObject);

    Then when the debug shows in the console, click on it to see what gameobject flashes to know if you have a copy on the wrong object.