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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Array length 0 only when I try to do something with it

Discussion in 'Scripting' started by kcfos, Mar 8, 2019.

  1. kcfos

    kcfos

    Joined:
    May 20, 2018
    Posts:
    16
    I'm building some ui that spawns buildings and shows a 3d preview of the selected building. The problem is when try to switch to cycle to the left, my array of preview objects is length 0. Every other frame on update says that it's length 3, which is what it should be.

    I'm calling left() through a ui button.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class buildingSpawner : MonoBehaviour
    6. {
    7.     public StatueScript ss;
    8.     public GameObject[] buildings;
    9.     private int size;
    10.     public GameObject[] buildPreview;
    11.     public GameObject thePreview;
    12.     public string[] names;
    13.     public int[] unlockPrices;
    14.     public int selectIndex;
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         selectIndex = 0;
    19.         //size = buildPreview.Length;
    20.         //Debug.Log("Size: " + size);
    21.     }
    22.  
    23.     public void openBuildMenu()
    24.     {
    25.         Cursor.visible = true;
    26.         gameObject.SetActive(true);
    27.         thePreview = Instantiate(buildPreview[selectIndex],transform);
    28.         Debug.Log("size on openBuildMenu: " + buildPreview.Length);
    29.     }
    30.  
    31.     public void left()
    32.     {
    33.         Debug.Log("going left selectIndex = " + selectIndex);
    34.         Debug.Log("size when going left: " + buildPreview.Length);
    35.         if (selectIndex > 0)
    36.         {
    37.             selectIndex -= 1;
    38.  
    39.         }
    40.         else
    41.         {
    42.             Debug.Log("selectIndex = 0, setting to size-1: " +buildPreview.Length +" -1");
    43.             selectIndex = buildPreview.Length-1;
    44.         }
    45.         Debug.Log("Selecting: " + selectIndex);
    46.  
    47.         //Destroy(thePreview);
    48.         thePreview = Instantiate(buildPreview[selectIndex],transform);
    49.  
    50.     }
    51.  
    52.     public void right()
    53.     {
    54.         if (selectIndex < buildPreview.Length-1)
    55.         {
    56.             selectIndex += 1;
    57.  
    58.         }
    59.         else
    60.         {
    61.             selectIndex = 0;
    62.         }
    63.         //Destroy(thePreview);
    64.         Debug.Log("Selecting: "+selectIndex);
    65.         Debug.Log("bp Length= " + buildPreview.Length);
    66.         thePreview = Instantiate(buildPreview[selectIndex],transform);
    67.  
    68.     }
    69.  
    70.     public void spawnBuilding()
    71.     {
    72.         Instantiate(buildings[selectIndex]);
    73.         PauseMenu.isPaused = false;
    74.         Cursor.visible = false;
    75.         Time.timeScale = 1f;
    76.         gameObject.SetActive(false);
    77.  
    78.     }
    79.  
    80.     public void closeBuildMenu()
    81.     {
    82.         Cursor.visible = false;
    83.         PauseMenu.isPaused = false;
    84.         ss.choosing = false;
    85.         //Destroy(thePreview);
    86.         Time.timeScale = 1f;
    87.         gameObject.SetActive(false);
    88.     }
    89.  
    90.     // Update is called once per frame
    91.     void Update()
    92.     {
    93.         Debug.Log("Size on update = "+buildPreview.Length);
    94.     }
    95. }
    96.  
    I've thought that maybe I'm changing something in another script, but I've checked the usual suspects and I can't find anything. If anyone has any ideas I'd be grateful. Attached is a png of the debug console.
     

    Attached Files:

  2. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Well right before the error it shows your "selectIndex" is -1
    upload_2019-3-8_18-21-5.png
    So you're trying to access buildPreview array's index of "-1" which doesn't exist (cause arrays go from 0 up).
     
    kcfos likes this.
  3. kcfos

    kcfos

    Joined:
    May 20, 2018
    Posts:
    16
    Yes but I don't know what's causing the array.length, which is what determines the array index to go to 0, for instance if i manually select 2 as my index, I'll get array index out of bounds because there's nothing in the array for that frame
     
  4. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    What is that script attached to? From your console log, it appears that buildPreview goes from length 3 to length 0, then back to length 3. One explanation might be that you are not looking at the same instance of your buildingSpawner class from inside left as you are from Update. That is, whatever buildingSpawner is attached to might be instanced a second time at run time. If that second instance runs left, it would probably have a zero-length buildingSpawner at that time.

    Try adding
    Debug.Log(gameObject.name)
    to Update and also to left, and let's see if they are being called for the same instance.
     
    st_helicoprion and kcfos like this.
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Even better would be to use a second parameter: Debug.Log(gameObject.name, gameObject).

    That way when you click on the log statement in your console, it will select the corresponding game object in your object hierarchy. This makes it easier to find the object in question, and is much more helpful if for any reason you have multiple objects with the same name.
     
    kcfos likes this.
  6. kcfos

    kcfos

    Joined:
    May 20, 2018
    Posts:
    16
    Seems to be the same object. I've set selectIndex to always be 0 but that doesn't work.
     

    Attached Files:

  7. kcfos

    kcfos

    Joined:
    May 20, 2018
    Posts:
    16
    Alright so I tried this, and what happens is when I click on the update debug log it highlights my buying panel object in yellow in the object hierarchy.

    When I click on the debug log called on left, nothing is highlighted.

    Searching for "buying panel" in the hierarchy only brings up the one.
     
  8. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Antistone's suggestion is a good one. As Denisowator mentioned, no index is going to avoid the exception when your array is of length zero. We need to figure out if it really is changed and, if so, why.

    Let's try something to confirm you are where you think you are: have your left method pause the editor by adding
    Debug.Break()
    as its first line. When your code hits that line, you can use the Inspector to verify if your array has actually changed length. I'm sure it has (or you wouldn't be seeing what you're seeing), but let's try that and see if it shows us anything else, like some other objects in the Project tree, etc.
     
    kcfos likes this.
  9. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    That still has me thinking you've got a fleeting instance of some object coming and going. Try my break trick and let us know what you get.
     
    kcfos likes this.
  10. kcfos

    kcfos

    Joined:
    May 20, 2018
    Posts:
    16
    Ok fixed it,

    turns out I was referencing the prefab buying panel instead of the buying panel in the scene, so you were right about there being two different versions.

    The fix was when selecting the on click() object for my button, to select scene and the buying panel object in that one.
     
    Stevens-R-Miller and Denisowator like this.
  11. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Glad you solved it! Now, there's an important lesson here for all of us: the bug wasn't in the code you posted, it was in the caller to that code. It's a hard thing to decide just how much code to post when you have a lot of it. In this case, you posted a small amount, but the problem was elsewhere. If you hadn't found the problem yet, my next suggestion would have been to post the calling code, as that's where my suspicions were. But, you got there on your own, so well done!
     
    Denisowator and kcfos like this.
  12. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    I'm a bit surprised that Unity even allows click events in a scene to point to methods on a prefab...
     
  13. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Well, we never saw his calling code, so we can't be sure exactly what was happening. But there may be some reason to operate directly on a prefab, maybe in particular with the new prefab workflow. Something to keep in mind, though.