- Home /
Constructors make my items null
I'm trying to make some new variables, but everytime i try to access them they end up being null, but for some reason in the inspector they show up.
The class is very simple.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Character
{
public string _name;
public Item[] _wpslot;
public Item _skill;
bool locked;
public Character()
{
_name = "";
_wpslot = new Item[3];
_skill = new Item();
}
public Character(string n)
{
_name = n;
}
}
Everything goes well except the Item class which ends up null, this is the class:
using System.Collections;
using UnityEngine;
[System.Serializable]
public class Item
{
public string _name;
public int m_id;
public int m_uid;
public string m_desc;
public string _iconpath;
public int m_resellprice;
public ItemType _type;
public enum ItemType
{
//Clothing
Hair,
Face,
Accessory,
Top,
Bottom,
Gloves,
Boots,
//Offensive
Weapon,
Skill,
//Consumables
Consumable,
//Misc
Miscellaneous
}
public Item()
{
_name = "Empty";
m_id = 0;
m_uid = -1;
m_desc = "Empty";
_iconpath = "ItemPlaceholder";
_type = ItemType.Miscellaneous;
}
public Item(Item i)
{
_name = i._name;
m_id = i.m_id;
m_uid = i.m_uid;
m_desc = i.m_desc;
_iconpath = i._iconpath;
m_resellprice = i.m_resellprice;
_type = i._type;
}
}
And then if i print the Item array for example, it prints null, so i can't access the 3 item array, however everything shows fine in the inspector.
I even did New Item() just before accessing it and i still got it null, i don't know why this does happens.
Any ideas of why does this happen? Thanks in advance :P
Don't you have to inherit from $$anonymous$$onoBehaviour or ScriptableObject for your classes to work properly in the editor?
You make an array of classes but never actually assign objects to the array, i.e.
_wpslot=new Item[3];
for(int i=0; i<_wpslot.Length; i++) {
_wpslot[i]=new Item();
}
I tried that and it still has the same error, if i try to access the array it throws a null exception.
However, i tried it doing it out of the constructor and it did work, why? i don't know.
Character c = new Character(name);
c._wpslot = new Item[3];
for (int i = 0; i < c._wpslot.Length; i++)
{
c. _wpslot[i] = new Item();
}
Apparently this does work, but the same thing on the constructor does not.
About ScriptableObject, never heard about it, going to check it out.
EDIT: Didn't solve anything unfortunately.
@maccabbe is right, though. An exception on trying to access an element _wpslot[] is exactly what one would expect from the code you've shown us, and the solution maccabbe has given should prevent that exception from occuring.
There are lots of other things that could be going on (as well). So, what line of code is actually throwing the exception?
You are putting the code that sets things up into the constructor that you're actually using, right?
I just noticed that in your code in the question you're only doing the set-up stuff in the first constructor (the one that takes no parameters), but in the code in your comment you're using the second constructor. The second one (in the code in the question) takes a string as input but then only sets the value of _name.
Try something like this, it removes duplication and makes it harder to forget to add new set-up code to all code-paths...
public Character()
{
init("");
}
public Character(string n)
{
init(n);
}
private init(string n)
{
_name = n;
_skill = new Item();
_wpslot=new Item[3];
for(int i=0; i<_wpslot.Length; i++)
{
_wpslot[i]=new Item();
}
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do you use serialized values at creation? 1 Answer
C# Syntax Question 3 Answers
how to construct all arrays (classes) with default constructor (not possible) 2 Answers