• 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 cloudsmeet · Sep 08, 2012 at 09:16 PM · nullreferenceexceptiononmousedown

OnMouseDown() NullReferenceException

I'm getting an error message on the following two scripts and I can't figure out why:

 #pragma strict
 
 var game : GameObject;
 
 function Start()
 {
     game = GameObject.Find("game");
 }
 
 function OnMouseDown()
 {
     game.SendMessage("addNewObject", this.name);
     Destroy(gameObject);
 }


And the second script is:

 var objectList : String[] = new String[10];
 var inventoryList : String[] = new String[10];
 var listLength : int = -1;
 
 function addNewObject(object : String)
 {
     objectList[++listLength] = object;
     inventoryList[listLength] = object;
 }

The first is a script called 'interactable.js' to be attached to a number of objects that the player can click on to add to an inventory as well as a list of objects to be deleted when the player next enters that scene, most of which isn't written yet.

The second is a script called 'game.js' attached to an object called 'game' which has another script that will cause it not be destroyed on load and will destroy itself if a previous version of itself exists.

The line I'm getting a null reference exception on is the `game.SendMessage("addNewObject", this.name);`

To be specific, this is the Null Reference Exception:

 NullReferenceException
 UnityEngine.GameObject.SendMessage (System.String methodName, System.Object value) (at C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:257)
 interactible.OnMouseDown () (at Assets/Scripts/interactible.js:32)
 UnityEngine.SendMouseEvents:DoSendMouseEvents()

What's going on?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DESTRUKTORR · Sep 08, 2012 at 09:22 PM

"this.name" this appears to be your problem area. Use gameObject.name, in stead. The "this" keyword returns the component, not the GameObject it's attached to, thus, it returns the name of the class, and not the GameObject.

Comment
Add comment · Show 5 · 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 cloudsmeet · Sep 08, 2012 at 09:42 PM 0
Share

I still get the Null Reference Exception even after those changes. I thought that might have been an issue beforehand and tried both passing nothing but "addNewObject", as well as trying to pass a string in place of gameObject.name and those didn't work either.

avatar image DESTRUKTORR · Sep 08, 2012 at 10:32 PM 0
Share

can you copy the 32nd line in your script(or whatever line the null reference is co$$anonymous$$g from, now, if it's changed), for me? Code lines on here don't seem to line up very well with how they're set up in monodevelop.

avatar image cloudsmeet · Sep 08, 2012 at 10:37 PM 0
Share

The 32nd line is

game.Send$$anonymous$$essage("addNewObject", gameObject.name);

It's the 32nd line because of a bunch of commented out code that I was redoing entirely and just copying choice bits out of.

avatar image cloudsmeet · Sep 09, 2012 at 12:15 AM 0
Share

I seem to have gotten around the issue by making the 'game' object the parent of the other object and using transform.root.gameObject.Send$$anonymous$$essage("addNewObject", gameObject.name);

I'd still be interested to know what's going on that's wrong, however.

avatar image DESTRUKTORR · Sep 09, 2012 at 03:13 PM 0
Share

Well, if you did what I said to do, then the only other possibility is that game was the null object, and the solution you came to only further supported that. Are you sure it was spelled correctly? $$anonymous$$ind any capital letters, as that will make a difference.

The only reason your original code wouldn't work (after my original suggested revisions) would be if either the GameObject, "game," had a different spelling for its name(possibly even a different name, entirely), or it didn't exist in the hierarchy, at all.

avatar image
0

Answer by Snake_Tn · Dec 16, 2012 at 07:54 AM

I got the same problem , the problem is when you attached the 'game' to this atribute 'var game : GameObject' from the unity Inspector , make sure that you selected the right one , select the game from hierarchy , and not from Assets(in case you have a prefab of 'game')

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

NullReferenceException after AddComponent in OnMouseDown 0 Answers

Gameobject.find & my error NullReferenceException: Object reference not set to an instance of an object 1 Answer

NullReferenceException 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

Instantinate NullReferenceError problem 0 Answers


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