When pressing the play button, my player controller immediately becomes disabled, I assume it’s something to do with the error message:
NullReferenceException: Object reference not set to an instance of an object
Player.PlayerController.Awake () (at Assets/Scripts/PlayerController.cs:57)
private SpellSlot[] SpellArray;
...
void Awake()
{
CameraLocked = false;
rb = GetComponent<Rigidbody>();
SpellArray = new SpellSlot[4];
int Y = Screen.height - 110;
SpellArray[0].GUISize = new Rect(10, Y, 100, 100); //Line 57
SpellArray[1].GUISize = new Rect(120, Y, 100, 100);
SpellArray[2].GUISize = new Rect(230, Y, 100, 100);
SpellArray[3].GUISize = new Rect(340, Y, 100, 100);
}
I can’t tell what’s actually wrong with this. Supposedly the array of objects isn’t instantiated, but it very clearly is.
EDIT: SpellSlot is a class with a constructor. Is the constructor not being called on instantiation?
If SpellSlot is class you need to create instance of it.
SpellArray = new SpellSlot[4];
int Y = Screen.height - 110;
int temp = 10;
int length = SpellArray.Length;
for (int i = 0; i < length; i++)
{
SpellArray[i] = new SpellSlot();
// You'll achieve the same result, but it's better solution than "manual" writing.
SpellArray[i].GUISize = new Rect(temp + (i * 110), Y, 100, 100);
}
I’ve made some changes, but the error is still occurring.
I’ll show everything related to the issue
public SpellSlot[] SpellArray;
...
void Awake()
{
CameraLocked = false;
rb = GetComponent<Rigidbody>();
int length = SpellArray.Length;
for (int i = 0; i < length; ++i)
{
SpellArray[i] = new SpellSlot();
}
}
...
void OnGUI()
{
int Y = Screen.height - 110;
int length = SpellArray.Length;
for (int i = 0; i < length; ++i)
{
GUI.Box(new Rect(10+(i*110), Y, 100, 100), SpellArray[i].name); //This line is now where the error points
}
}
and what’s left of the SpellSlot class after taking it apart trying to solve this issue
public class SpellSlot : MonoBehaviour
{
public int ID;
public string Name;
public bool Occupied;
public SpellSlot()
{
ID = 0;
Name = "";
Occupied = false;
}
}
Of course, I didn’t realize (or think to ask) if SpellSlot is a MonoBehaviour. If it doesn’t have to be one, then do not make it inherit from (that).
If you want it to be one, you cannot create a mono behaviour with the ‘new’ keyword. You must AddComponent to some game object.
You’ll have to know/decide what you want to do with that.
As methos5k is saying, a MonoBehaviour needs to be a component attached to a GameObject. If that is what you want, then you should create a prefab of a GameObject with this SpellSlot component attached, and instead of creating new SpellSlot instances you instead instantiate new GameObjects from that prefab. Then to access the SpellSlot component you would use GetComponent() on that instantiated GameObject.
Still giving
NullReferenceException: Object reference not set to an instance of an object
Player.PlayerController.Awake () (at Assets/Scripts/PlayerController.cs:55)
This time pointing to the line
int length = SpellArray.Length;
I spotted that and quickly deleted that post
commenting out the SpellArray.Length lines and just using a constant in place of length now gives the following
NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) object:stelemref (object,intptr,object)
Player.PlayerController.Awake () (at Assets/Scripts/PlayerController.cs:58)
//int length = SpellArray.Length;
for (int i = 0; i < 4; ++i)
{
SpellArray[i] = new SpellSlot(); //Error now points to this line
}
I highly suggest going through some very basic C# tutorials. Stuff like how to properly declare an array in C#. In your case you are trying to use SpellArray before you have initialized it, based on the line of code you say the error is on.
Why are they so bloody awkward to work with? C# isn’t my first language, but it has a reputation of being easy to learn, yet using an array of objects is so incredibly unintuitive
And I’ve read these tutorials, but they don’t seem to be at all relevant
Regardless, I’ve changed the declaration of SpellArray and it works now, but I can’t say I’ve learnt much from the experience