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:
public enum Attributes
{
Endurance,
Strength,
Dexterity,
Agility,
Intelligence,
Willpower
}
create the following class:
[System.Serializable] public class Attribute
{
[System.NonSerialized]
public CharacterStats parent;
public Attributes type;
public ModifiableInt value;
public void SetParent(CharacterStats _parent)
{
parent = _parent;
value = new ModifiableInt(AttributeModified);
}
public void AttributeModified()
{
parent.AttributeModified(this);
}
}
and I then inside the CharacterAttributes class I create an array to hold the attributes:
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:
public Attribute[] attributes;
in CharacterStats I have tried using:
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:
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:
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!