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 Fetdressing · Apr 15, 2013 at 03:45 PM · collideroverlapsphere

Area of effect

Hey! I am working on a tower defence kind of game and I am trying to make projectiles deal damage in an area. When the projectiles hit the targets I get: "Object reference not set to an instance of an object". Here is my script:

 public var rad : float = 10;
 private var center : Vector3; 
  
 private var hitTarget : GameObject;
 
 public var damage : float = 10;
 function Update () {
 
 }
 function OnCollisionEnter(hitTarget : UnityEngine.Collision){ 
     if(hitTarget.gameObject.tag == "Enemy"){
     var objectHit = hitTarget.gameObject; 
     //Callhealth = GameObject.Find(objectHit).GetComponent(Health);
     objectHit.GetComponent(Health).applyDMG(damage);
     areaOfEffect(transform.position, 10);
     //objectHit.SendMessage("applyDMG", 10);
         //Callhealth.applyDMG(damage);
         Destroy(this.gameObject);
     }
     
 }
 function areaOfEffect(center : Vector3, rad : float){
     var hitEnemies : Collider[] = Physics.OverlapSphere(center, rad); //från mittpunkten och rad:s värde utåt 
     
     for(var x=0; x < hitEnemies.Length; x++){
         hitEnemies[x].GetComponent(Health).applyDMG(damage);
     }
 }

Any kind of help would be much appreciated! :) Thanks in advance.

Comment
Add comment · Show 1
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 iwaldrop · Apr 15, 2013 at 05:44 PM 0
Share

It is helpful to know what object you're getting the null reference exception on. Can you post the whole error message that Unity generates?

But, I think it's probably the Health component, so I'll take a stab at an answer.

1 Reply

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

Answer by iwaldrop · Apr 15, 2013 at 05:55 PM

You need to do a check to make sure that your Health Component is actually attached to the object that you've found; the ground, for instance, shouldn't have one...right? :)

 function OnCollisionEnter(hitTarget : UnityEngine.Collision)
 {
     if(hitTarget.gameObject.tag == "Enemy")
     {
         var objectHit = hitTarget.gameObject; 
         //Callhealth = GameObject.Find(objectHit).GetComponent(Health);
         var health = objectHit.GetComponent(Health);
         if (health != null)
         {
             health.applyDMG(damage);
             areaOfEffect(transform.position, 10);
             //objectHit.SendMessage("applyDMG", 10);
             //Callhealth.applyDMG(damage);
             Destroy(this.gameObject);
         }
     }
 }
Comment
Add comment · Show 3 · 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 Fetdressing · Apr 15, 2013 at 06:02 PM 0
Share

Aha! Though I am still getting the same error message in the areaOfEffect function at the line: hitEnemies[x].GetComponent(Health).applyD$$anonymous$$G(damage); it must somehow also get ground as hit target it seems. Got any ideas on how to solve that? Thanks!

avatar image iwaldrop · Apr 15, 2013 at 06:09 PM 0
Share

Well, you're going to have to implement a check for Health being null any time you're grabbing random gameObjets and then trying to do something with Health.

Always create a local reference so that you can check that the type you're looking for is present.

 SomeScript s = GetComponent(typeof(SomeScript));
 if (s != null)
 {
     // Do something to s.
 }
avatar image Fetdressing · Apr 15, 2013 at 06:23 PM 0
Share

This works, atleast for the moment:

 function areaOfEffect(center : Vector3, rad : float){
         var hitEnemies : Collider[] = Physics.OverlapSphere(center, rad); //från mittpunkten och rad:s värde utåt 
         
         for(var x=0; x < hitEnemies.Length; x++){
         if(hitEnemies[x].gameObject.tag == "Enemy"){
             hitEnemies[x].GetComponent(Health).applyD$$anonymous$$G(damage);
             }
         }
     }
 

Do you think this could cause problems at some point or should it work perfectly?

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

12 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

Related Questions

overlapsphere to destroy NPCs on exit 1 Answer

OverlapSphere seems to detect everything and ignores tags. How can I fix this? 1 Answer

OverlapSphere returned collider arrays 1 Answer

Objects overlapping at origin (0,0,0) (question about OverlapSphere?) 0 Answers

How come certain Children don't cast an Overlapsphere? 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