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
  4. Dismiss Notice

Trouble initializing arrays of a certain size.

Discussion in 'Scripting' started by Latheeb, Dec 4, 2020.

  1. Latheeb

    Latheeb

    Joined:
    Jun 27, 2016
    Posts:
    21
    I'm not incredibly experienced with Unity, so I apologize if I misuse any terminology in here or if this seems very simple.

    I'm using arrays to store my stats and their values and such for characters in my game and everything is working fine, but I currently have to set the size of the arrays for these stats in the inspector for each character in the game. I imagine this could become tedious and the source of frustration in the future so I was trying to figure out how to set the length of these arrays using my scripts that handle the character stats.

    So I currently have a script called "Character Stats" where:

    I create an enum with these lines:

    Code (CSharp):
    1. public enum Attributes
    2. {
    3.     Endurance,
    4.     Strength,
    5.     Dexterity,
    6.     Agility,
    7.     Intelligence,
    8.     Willpower
    9. }
    create the following class:

    Code (CSharp):
    1. [System.Serializable] public class Attribute
    2. {
    3.     [System.NonSerialized]
    4.     public CharacterStats parent;
    5.     public Attributes type;
    6.     public ModifiableInt value;
    7.     public void SetParent(CharacterStats _parent)
    8.     {
    9.         parent = _parent;
    10.         value = new ModifiableInt(AttributeModified);
    11.     }
    12.     public void AttributeModified()
    13.     {
    14.         parent.AttributeModified(this);
    15.     }
    16. }
    and I then inside the CharacterAttributes class I create an array to hold the attributes:

    Code (CSharp):
    1.     public Attribute[] attributes;
    All of this works as intended. I've tried the following changes in an attempt to get things to work with setting the array length:

    instead of the line:

    Code (CSharp):
    1.     public Attribute[] attributes;
    in CharacterStats I have tried using:

    Code (CSharp):
    1.     public Attribute[] attributes = new Attribute[6];
    and I have tried going into my CharacterStatsController script (which inherits from CharactarStats and is the script that actually sits on the character) and adding:

    Code (CSharp):
    1. attributes = new Attribute[6];
    within the Start() method.

    Both of these changes create the array with the proper size, but I get a NullReferenceException error from the following line of my CharacterStatsController script:

    Code (CSharp):
    1. attributes[0].type = Attributes.Agility;
    where I am trying to change the element within the array's type so that it is the appropriate attribute.


    I'm sure I'm simply not understanding something and I apologize if my explanation of my problem or what I'm trying to accomplish is confusing, but I've been trying for a little while and cannot find anything helpful. I'd really appreciate it if anyone could explain what I'm doing wrong and how I could do it correctly or point me toward a video or an article explaining how to do this.

    Thanks a lot!
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    When you create an array you need to initialize all the values in the array to use them.

    Code (CSharp):
    1. attributes[0] = new Attribute();
    2.  
    3. //or do them all
    4.  
    5. for (int i = 0; i < 6; i++)
    6. {
    7.     attributes[i] = new Attribute();
    8. }
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    NullReferenceException means that the variable hasn't been assigned a real value yet. In addition to creating the array itself, you also need to create Attribute objects to put IN the array.
    Code (CSharp):
    1. attributes = new Attribute[6];
    2. attributes[0] = new Attribute();
    3. attributes[1] = new Attribute();
    4. attributes[2] = new Attribute();
    5. attributes[3] = new Attribute();
    6. attributes[4] = new Attribute();
    7. attributes[5] = new Attribute();
    (You would normally do that with a loop, but I'm being explicit for clarity.)

    If you want to fill in the data from the inspector, though, then doing this in Start() would be a mistake, because Start() runs after Unity fills in the data that you enter in the inspector. Rather than getting your array ready for the inspector to modify, you'll actually be overwriting all the data from the inspector with a brand new blank array. If you want to create the array in script like this, you should control the data entirely from script, and not rely on the inspector at all.
     
  4. Latheeb

    Latheeb

    Joined:
    Jun 27, 2016
    Posts:
    21
    That makes perfect sense. Thanks so much to both of you! I knew it was something really simple messing me up.