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 MichaellaM · Dec 06, 2014 at 10:31 PM · c#objectreferenceinstance

"Object reference not set to an instance of an object"

Hello everybody.

I'm making a 2D game that likes a tower defence. I'm writing the enemy's AI scripts that will go to the planet and start to attack it. But I can't make the enemies take out the planet's life, and I don't know why...

I'm new in this, can you help me?

Script called "EnemyAI" it's the enemies' AI, the "PlanetLife" it's the planet's life.

Enemies AI's code:

 using UnityEngine;
 using System.Collections;
 using DG.Tweening;
 
 public class EnemyAI : MonoBehaviour {
 
     public Transform target;
     NavMeshAgent spaceship;
     public Vector3 distTarget;
 
     private PlanetLife life;
 
     public enum States
     {
         Invading,
         Hitting,
         Attacking
     }
 
     public States state = States.Invading;
 
     void Awake()
     {
         life = GetComponent<PlanetLife> ();
     }
 
     void Start ()
     {
         spaceship = GetComponent<NavMeshAgent> ();
     }
 
 
     void Update () {
 
         switch(state)
         {
 
         case States.Invading:
             StateInvading();
 
             break;
 
         case States.Attacking:
             StateAttacking();
              
             break;
 
         case States.Hitting:
             StateHitting();
 
             break;
 
         default :
             Debug.LogError("BUG: Cannot stay without state.");
          
             break;
          
         }
 
     }
 
     private void StateInvading()
     {
         distTarget = target.position - transform.position;
         spaceship.SetDestination (target.position);
 
         if (distTarget.magnitude < 15)
         {
             state = States.Hitting;
         }
     }
 
     private void StateHitting()
     {
  
         spaceship.Stop ();
         life.LIFE -= 10;
         Debug.Log (life.LIFE + " remaining lives.");
 
     }
  
 
     private void StateAttacking()
     {
 
     }
 }


Planet's life code

 using UnityEngine;
 using System.Collections;
 
 public class PlanetLife : MonoBehaviour
 {
     public float LIFE;
  
     public void Start ()
     {
         LIFE = 1000;
     }
 }


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 EvilTak · Dec 07, 2014 at 07:29 AM 0
Share

Can you post the error which comes in the console? That way we'll know exactly what is causing the problem. Just make sure that all of your public variables are assigned in the inspector and the gameObjects which have the EnemyAI script both have a Nav$$anonymous$$eshAgent and a PlanetLife component.

avatar image MichaellaM · Dec 07, 2014 at 10:11 PM 0
Share

Evil Tak this is the error: "NullReferenceException: Object reference not set to an instance of an object EnemyAI.StateHitting () (at Assets/Scripts/EnemyAI.cs:77)EnemyAI.Update () (at Assets/Scripts/EnemyAI.cs:49)" And all variables are assigned... I don't know why it's happening...

2 Replies

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

Answer by static_cast · Dec 07, 2014 at 10:15 PM

There error is probably here: life = GetComponent();

I think what you mean to say is life = target.gameObject.GetComponent();

The reason is that you are getting the current gameObjects component, rather than the target's.

Hope this helps! ;)

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 MichaellaM · Dec 07, 2014 at 10:28 PM 0
Share

Thank you so much! It's worked!! :D

avatar image static_cast · Dec 07, 2014 at 10:32 PM 0
Share

No problem. :)

avatar image
1

Answer by Baste · Dec 07, 2014 at 10:20 PM

Your error is telling you that this line (77) in StateHitting() is causing the error:

 life.LIFE -= 10;

That line causing a nullReference means that life is null - not assigned to anything.

You're fetching the life variable in Awake like this:

 life = GetComponent<PlanetLife> ();

If life is null after that, the GetComponent call found no PlanetLife script on the same object as the EnemyAI script is on. Which makes sense - it doesn't sound like they belong in the same place.

If you're only going to have a single PlanetLife script in your scene, replace GetComponent with FindObjectOfType, and you'll be golden. Otherwise, you'll need to rethink what you're doing - but the bottom line is that GetComponent doesn't find any component with that class, only components attached to the same object in the scene.

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 static_cast · Dec 07, 2014 at 10:22 PM 0
Share

He alread has an instance of the planet in the target transform, so FindObjectOfType would be costly and he would have to figure out which planet to target with more planets in the scene.

avatar image MichaellaM · Dec 07, 2014 at 10:29 PM 0
Share

Thanks so much you both, I understood my error, and fixed it! :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Script decides not to run anymore 1 Answer

Object reference not set to an instance of an object 0 Answers

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

i don't understand "NullReferenceException: Object reference not set to an instance of an objec" 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