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 fireboy0526 · Apr 22, 2015 at 01:38 PM · objectnullreferenceexceptioninheritance

Object Inheritance NullReferenceException: Object reference not set to an instance of an object

0 I have a script called GreedySearch where it contains the following function printMove().

  public class GreedySearch : MonoBehaviour {
      public void printMove(){
          Debug.Log ("GS printMove()");
      }
  
  }

 

Now I have a main controller called EnemyBrain which defines the following like this:

  public class GreedySearch : MonoBehaviour {
      public GreedySearch gSearch;
  
      public virtual void Start () {
          gSearch = GetComponent<GreedySearch> ();
      }
 
      public void moveNPC() {
          gSearch.printMove();
      }
  }

 
 

Now I have a Enemy script that inheritance EnemyBrain, and I'm trying to call gSearch.printMove(), however I'm getting the error

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

This is my Enemy script

  public class Enemy1 : EnemyBrain {
      public override void Start() {
          base.Start ();
      }
  
      void FixedUpdate() {
          moveNPC();
      }
  }

 

The error shows that it comes from EnemyBrain at gSearch.printMove();.

My code does a little bit more, but I think this simple example should be able show where my errors are coming from.

Any help is very much appreciated.

Thank you

Comment
Add comment · Show 4
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 NoseKills · Apr 22, 2015 at 02:16 PM 0
Share

Just to make sure... GreedySearch and Enemy1 scripts are on the same GameObject?

avatar image nicolasjr · Apr 22, 2015 at 02:23 PM 0
Share

Try not overriding your start method. If you're just calling the base, there's no reason to override it.

But the problem is that probably there's no such component in the same gameObject. Are you sure your GreedySearch is at the same GameObject as the EnemyBrain script?

avatar image fireboy0526 · Apr 22, 2015 at 03:09 PM 0
Share

Hello Nose$$anonymous$$ills and nicolasjr, thanks for the reply. I am sure that I have all 3 scripts on my object, which is why this error looks very weird to me.

avatar image nicolasjr · Apr 22, 2015 at 03:37 PM 0
Share

Well, do a verification, so that the method will only be called if the pointer is not null. That's good practice, anyways. Perhaps in the first frame your method is being called before starting the instance, hence the error.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SUMFX · Apr 22, 2015 at 03:45 PM

You are calling same object from itself:

 public class GreedySearch : MonoBehaviour {
     public GreedySearch gSearch;

If this is just a typo then two components EnemyBrain and GreedySearch must be on same gameobject since you are trying to get GreedySearch component without declaring parent:

 public virtual void Start () {
     gSearch = GetComponent<GreedySearch> ();
 }
Comment
Add comment · Show 1 · 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 nicolasjr · Apr 22, 2015 at 03:53 PM 0
Share

based on the whole post text, It seems to be a typo. He even says it's the EnemyBrain class.

Furthermore, the code wouldn't run, since there would exist two GreedySearch classes.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException help 1 Answer

[Closed]NullReferenceException on Object Instantiation onto game world 2 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Pause menu error NullReferenceException:Object reference not set to an instance of an Object .js 1 Answer

NullReference Exception when adding a class 2 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