• 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 Reagix · Jul 04, 2015 at 07:49 AM · nullreferenceexceptiononmousedownaddcomponent

NullReferenceException after AddComponent in OnMouseDown

Hi,

I'm new to Unity and C#, so please let me know if I'm missing something obvious/correct obvious style errors. I'm getting a null reference exception in OnMouseDown below (apparently room_manager_ is null):

 public class Excavatable : Room {
     private const string kPrefabPath = "ExcavatablePrefab";
 
     public static Excavatable Create(Vector2 coordinates, RoomManager room_manager) {
         Excavatable new_excavatable =
             room_manager.gameObject.AddComponent<Excavatable>();
         new_excavatable.InitializeRoom(coordinates,
                                        Resources.Load (kPrefabPath) as GameObject,
                                        room_manager);
         return new_excavatable;
     }
 
     protected override void OnMouseDown() {
         base.room_manager_.Excavate(this);
     }
 }
 

Where Room.InitializeRoom is defined as:

 protected RoomManager room_manager_;
 protected void InitializeRoom (Vector2 coordinates, GameObject game_object, RoomManager room_manager) {
     ...
     room_manager_ = room_manager;
     Instantiate(game_object, position_, Quaternion.identity);
 }

And Excavatable.Create is called as (in RoomManager):

     void MakeExcavatable(Vector2 coordinates) {
         rooms_[coordinates] = Excavatable.Create(coordinates, this);
     }


I'm at a loss here. In InitializeRoom, room_manager is not null (I checked), but it is null when OnMouseDown is clicked. I'm not calling it on a different instantiation, since I only instantiated one Excavatable. The only thing I can think of right now is that maybe InitializeRoom is somehow not modifying the component?

Any help would be greatly appreciated! Thanks.

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

NullReferenceException - Editor script using GetComponent after AddComponent 1 Answer

OnMouseDown() NullReferenceException 2 Answers

AddComponent giving NullReferenceException in Editor script 1 Answer

AddComponent() throwing NullReferenceException 2 Answers

NullReferenceException when sending message to another object...really stumped. 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