• 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
Question by P2ThatGuy · Aug 04, 2013 at 07:04 PM · javascriptexplosionbombgrenadephysics.overlapsphere

OverlapSphere not causing damage? - Solved

I've been trying to use Physics.OverlapSphere so my grenades do damage to the enemy. Unfortunately, I'm unable to cause damage to the enemies in my game, any help is appreciated.

Current Script (using JavaScript) :

 public var explosion : GameObject;
 
 public var timer : float = 10.0;
 public var radius : float = 5.5;
 public var force : float = 25.0;
 
 public var throwForce = 150.0;
 
 public var detonate : boolean = false;

 function Start () {
 
 rigidbody.AddRelativeForce(0, 0, throwForce);
 
 }

 function Update () {
 
 if(detonate){
     timer -= Time.deltaTime;
     }
     
 if(timer <= 0){
     Explode();
     }
 
 }

 function OnCollisionEnter () {
 
 detonate = true;
 
 }

 function Explode () {
 
 var explosionPos : Vector3 = transform.position;
 var Cols : Collider[] = Physics.OverlapSphere(explosionPos, radius);
 
 for (var hit : Collider in Cols){
 
     if(!hit){
         continue;
         }
         
     if(hit.rigidbody){
         hit.rigidbody.AddExplosionForce(force, explosionPos, radius, 3.0);
         }
         
     if(hit.transform.tag == "Enemy"){
         hit.transform.GetComponent("AiHealth").health -= 100;
         }
     }
 
 Instantiate(explosion, transform.position, Quaternion.identity);
 
 Destroy(gameObject);
 
 }
Comment

People who like this

0 Show 0
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

Answer by Jamora · Aug 04, 2013 at 07:36 PM

You are using hit.transform.tag, which won't work; only GameObjects have tags. So you need to replace that with hit.gameObject.tag

Same applies for the GetComponent below; use gameObject instead of transform.

Unless I missed something else (and assuming the rest of your code logic works) that should do it.

Comment

People who like this

0 Show 4 · 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 P2ThatGuy · Aug 04, 2013 at 08:02 PM 0
Share

that didn't work, I'm still getting a NullReferenceException in the console.

avatar image Jamora · Aug 04, 2013 at 09:06 PM 0
Share

I reviewed the RaycastHit documentation and it turns out you can't access the gameobject directly. You need hit.transform.gameObject.tag

avatar image P2ThatGuy · Aug 05, 2013 at 12:01 AM 0
Share

still not working :/

avatar image Jamora · Aug 05, 2013 at 09:07 AM 0
Share

What line are you getting the null reference on? A null reference means that a reference type variable is not set and thus points to empty (zero).

The only references I see in this code are explosion and hit.rigidbody.

Are you sure your clickable things have a rigidbody? Have you set the explosion GameObject in the inspector? Does the GameObject that script is attached to have a rigidbody?

avatar image

Answer by P2ThatGuy · Aug 06, 2013 at 11:23 AM

If anybody else is having a similar issue, I figured it out. Rather than a GetComponent, I put a function inside the ai script :

 function Damage (dmg : int) {
 
 health -= dmg;
     
 }

then in the script I previously posted replaced the enemy tag bit with this :

     if(hit.collider.gameObject.tag == "Enemy"){
         hit.collider.gameObject.SendMessageUpwards("Damage", 110, SendMessageOptions.DontRequireReceiver);
         hit.collider.gameObject.rigidbody.AddExplosionForce(force, explosionPos, radius, 3.0);
         }
     }

thanks for all help that was previously given.

Comment

People who like this

0 Show 0 · 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

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

15 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

Related Questions

Help With Remote Explosive 1 Answer

How do I destroy all enemy game objects with a bomb? 2 Answers

timed bomb, explosion detection 2 Answers

Explosion Damage not being applied. 1 Answer

Grenade Damage in Radius 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