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 LifeArtist · Apr 28, 2014 at 04:02 PM · errorinstancefix

NullReferenceException Error

Hey,

I have a problem with my enemyHealth script. I googled the error and found something that i have to make a new instance of the object. Maybe with ' new Object() ' or something else but i cant fix it >.<

 using UnityEngine;
 using System.Collections;
 
 public class enemyHealth : MonoBehaviour
 {
     Animator anim;
     enemyMovement enemyMove;
     enemySendDamage enemySendDmg;
 
 
     public int currentHealth = 20;
     public int storedExp = 10;
     public float actualTime;
     public Transform enemySprite;
 
     // Use this for initialization
     void Start()
     { 
         anim = enemySprite.GetComponent<Animator>();
         enemyMove = GetComponent<enemyMovement>();
         enemySendDmg = GetComponent<enemySendDamage>();
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     void applyDamage(int damage)
     {
         if (currentHealth > 0)
         {
             currentHealth -= damage;
             if (currentHealth < 0)
             {
                 currentHealth = 0;
             }
 
             if (currentHealth == 0)
             {
                 enemySendDmg.colObject.SendMessage("applyExp", storedExp);
                 die();
             }
             else
             {
                 anim.SetTrigger("hit");
             }
         }
     }
 
     void die()
     {
         enemyMove.enabled = false;
         anim.SetTrigger("hit");
         anim.SetTrigger("die");
         Destroy(gameObject,1.23f);
 
     }
 }


This is the full error message :

NullReferenceException: Object reference not set to an instance of an object enemyHealth.applyDamage (Int32 damage) (at Assets/Scripts/Enemy/enemyHealth.cs:42) UnityEngine.Component:SendMessage(String, Object, SendMessageOptions) charSendDamage:OnTriggerStay2D(Collider2D) (at Assets/Scripts/Character/charSendDamage.cs:42)

Regards,

LifeArtist

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
Best Answer

Answer by LifeArtist · Apr 28, 2014 at 08:48 PM

Instead of using enemySendDmg = GetComponent(); i have to use GetComponentInChildren<>();

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
avatar image
0

Answer by komodor · Apr 28, 2014 at 04:15 PM

the error says, that at line 42 you are calling something which does not exist or there is no reference to that

i guess that "enemySendDmg" has no "colObject" set

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 LifeArtist · Apr 28, 2014 at 04:24 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class enemySendDamage : $$anonymous$$onoBehaviour 
 {
 
     public int damageAmount = 10;
     public GameObject colObject;
     public Collider2D col;
 
     void OnTriggerEnter2D(Collider2D collider)
     {
         if (collider.tag == "Player")
         {
             colObject = collider.gameObject;
             collider.Send$$anonymous$$essage("applyDamage", damageAmount, Send$$anonymous$$essageOptions.DontRequireReceiver);
         }
     }
 
     void OnTriggerStay2D(Collider2D collider)
     {
         if (collider.tag == "Player")
         {
             colObject = collider.gameObject;
             collider.Send$$anonymous$$essage("applyDamage", damageAmount, Send$$anonymous$$essageOptions.DontRequireReceiver);
         }
     }
 
     void OnTriggerExit2D(Collider2D collider)
     {
         if (collider.tag == "Player")
         {
             colObject = collider.gameObject;
             collider.Send$$anonymous$$essage("applyDamage", damageAmount, Send$$anonymous$$essageOptions.DontRequireReceiver);
         }
     }
 }

This is the senddmg class and i thought i set the colObject.

( ty for the fast response ^^ )

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

20 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

Related Questions

4 Error on my script. 1 Answer

"Object Reference not set to an instance of an object" 1 Answer

very simple shoot script keeps getting error!!! 3 Answers

Fix this script? 0 Answers

How to check collision with specific object 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