Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sendatsu_Yoshimitsu · May 27, 2014 at 07:25 AM · c#gameobjectprogrammingsendmessage

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fornoreason1000 · May 27, 2014 at 08:16 AM 0
Share

Can you show me the code where lines 31 and 44 are? the error is inside your ActionBar class

avatar image Sendatsu_Yoshimitsu · May 27, 2014 at 08:33 AM 0
Share

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 () {
 
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sendatsu_Yoshimitsu · May 27, 2014 at 08:45 AM 0
Share

That works perfectly- thank you very much! :)

avatar image CHPedersen · May 27, 2014 at 08:49 AM 0
Share

You're very welcome. :) I took the liberty to convert your reply to a comment.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

"Center On Children" programmatically 1 Answer

C# Check If Gameobject is within Collider 1 Answer

Refering to gameobject script is attached to 1 Answer

C# Preserving GameObjects' Previous Meshes 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges