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

Need Help With Static Array

Discussion in 'Scripting' started by KDB44, May 9, 2017.

  1. KDB44

    KDB44

    Joined:
    Feb 12, 2017
    Posts:
    5
    Hey guys,

    I'm trying to get a basic inventory working. I have a static array and a health Item class. I was told to make my array static because the size will not change. However, the collision code is not working anymore. Nothing happens and I can no long see the array elements in the inspector.

    https://pastebin.com/wsp7SzuT

    I really would appreciate any help.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Nothing wrong with a static array but you cannot see static variables in the inspector for editing. I believe you can see them if you enable 'debug' mode, though. Not true, cannot be seen in debug mode.*edit*

    As for your code "not working". You create an array with a size of 5, but then your code says to do something if the array length is >= 9.. therefore , it never runs (unless that array is modified elsewhere I cannot see).

    Does that make sense?
     
    Last edited: May 9, 2017
  3. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    if you want to see static variables in the inspector then you'll need to write custom editor scripts to show it. not even Debug mode can show static variables. Debug mode simply iterates over all serialized properties of a class, and unity has zero support for serializing static variables (and with good reason, if you stop to think about it you can likely figure out why).
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    That is not a reason to make an array static. Static means it is a class variable rather than an instance variable.
     
  5. KDB44

    KDB44

    Joined:
    Feb 12, 2017
    Posts:
    5
    Thanks I see now.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool - wasn't sure about static + debug mode.. haven't used it much :)
     
  7. ChazBass

    ChazBass

    Joined:
    Jul 14, 2013
    Posts:
    153
    A primitive array (int[] myArray, MyClass[] myArray, etc) is of a fixed length that cannot be changed (the contents can be, of course). Primitive arrays are inherently mutable so there is no way to make its elements unchangeable through declaration (see link below for a work around).

    An ArrayList or generic List<Some Type> are dynamic (meaning you can add items to them and they resize appropriately). You cannot make their size fixed without using wrappers.

    If you want an array list to be readonly, you can do this:

    https://msdn.microsoft.com/en-us/library/e2abk8wd(v=vs.110).aspx