- Home /
Getting nullreference error while using sendmessage
I'm trying to iteratively generate 10 GUI.buttons to use as a skill bar . Each button's function needs to be customizeable, so I have a string array that I use in conjunction with sendmessage to indicate which skill's function (from a large database of functions I keep in a separate script) should be run when the button is activated. Right now, it reads as follows:
private string[] buttonAction;
void OnGUI(){
DisplayActionBar ();
}
public void DisplayActionBar(){
for (int cnt = 0; cnt < 10; cnt++) {
buttonAction[cnt] = "NoSkill";
if (GUI.Button (new Rect ((Screen.width / , (Screen.height - 100), 50, 50), blankActionTexture, "label")) {
gameObject.SendMessage (buttonAction[cnt]);
}
}
}
This doesn't pop an error while in unity, but when I run it, it tells me:
NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object) ActionBar.DisplayActionBar () (at Assets/Scripts/HUD Classes/ActionBar.cs:44) ActionBar.OnGUI () (at Assets/Scripts/HUD Classes/ActionBar.cs:31)
Two scripts are being used- Action Bar, which is the tiny function that draws my bar and lets me drag & drop icons, and ActionDatabase, which contains functions for each skill (and a debug function NoSkill that gets called if the player activates an empty skill box). Both scripts are attached to the same object, Is gameObject.sendmessage somehow not finding ActionDatabase?
Can you show me the code where lines 31 and 44 are? the error is inside your ActionBar class
Sure, the formatting will have shifted a bit since I've been cleaning and commenting, here's the entire script:
using UnityEngine;
using System.Collections;
public class ActionBar : $$anonymous$$onoBehaviour {
// private ActionDatabase actionDatabase; //Probably not relevant- relic from when I was experimenting with how getcomponent worked
private string[] buttonAction;
public Texture2D blankActionTexture;
public Texture2D actionTexture1; // Prototype texture to test icon setting
// Use this for initialization
Rect buttonRect = new Rect(10, 10, 100, 20); //Not in use yet-
bool buttonPressed = false;
void Awake(){
// actionDatabase = GetComponent<ActionDatabase>(); Relic from experimenting with getcomponent, same as above
}
void Start () {
}
void OnGUI(){
DisplayActionBar ();
}
public void DisplayActionBar(){
for (int cnt = 0; cnt < 10; cnt++) {
buttonAction[cnt] = "NoSkill";
if (GUI.Button (new Rect ((Screen.width / 8), (Screen.height - 100), 50, 50), blankActionTexture, "label")) {
gameObject.Send$$anonymous$$essage(buttonAction[cnt]);
}
}
}
// Update is called once per frame
void Update () {
}
}
Answer by CHPedersen · May 27, 2014 at 08:41 AM
There is no place in your code where you instantiate buttonAction. Based on how you iterate through it in the for-loop in method DisplayActionBar, I take it you expect it to be a string array of size 10. But where did you tell C# that? You're supposed to initialize it first:
buttonAction = new string[10];
If you don't, it will remained declared, so the reference exists and thus does not cause compiler errors, but not instantiated, so the reference points to null. When one attempts to use a reference type variable that points to null, you get a NullReferenceException.
That works perfectly- thank you very much! :)
You're very welcome. :) I took the liberty to convert your reply to a comment.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
"Center On Children" programmatically 1 Answer
C# Check If Gameobject is within Collider 1 Answer