• 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 namekaw · Mar 14, 2013 at 07:11 PM · javadamageotherscripthealth

Link Health Bar and TNT Damage scripts together

I have a script that is attached to a car which gives is health and has a bar associated with it. I also have TNT which causes damage. I am not sure how to make the TNT subtract damage from the car health. Here is my explosion script which I found in a tutorial and changed to the way I thought would work. It has a bit to connect scripts but I am not sure the correct syntax to make it work-

enum ExplosionTypes {TNT, Nitro, Other} var explosionType : ExplosionTypes; var explosion : Transform; var damage : float; var hitPoints : float = 0; var deadReplacement : Rigidbody; var explosionFX : AudioClip; private var explodeSecs : float = 1; private var duration : float = 1; private var isTNT = false;

Function OnCollisionEnter (col : Collision){

 var contact : ContactPoint = col.contacts[0];
 var rot : Quaternion = Quaternion.FromToRotation(Vector3.up,contact.normal);
 var pos : Vector3 = contact.point;
 
 if(col.collider.gameObject.tag == "Stockcar" || col.collider.gameObject.tag == "TNT"){
    if(explosionType == ExplosionTypes.Nitro){
      Instantiate(explosion,pos,rot);
      Explode();
      audio.PlayOneShot(explosionFX);
      Destroy(gameObject);
      col.collider.SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver);
      }
    if(explosionType == ExplosionTypes.TNT){
      isTNT = true;
    }
    if(explosionType == ExplosionTypes.Other){
      Instantiate(explosion,pos,rot);
      Explode();
      audio.PlayOneShot(explosionFX);
      Destroy(gameObject);
      col.collider.SendMessage("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver);
    }
    }
 

}

function Update () { var t : float = Mathf.PingPong (Time.time, duration) / duration; if(isTNT){ renderer.material.color = Color.Lerp(Color.red,Color.gray,t); } }

function Explode(){ var expl : Transform; expl = Instantiate(explosion,transform.position, transform.rotation); Destroy(gameObject); if(isTNT) isTNT = false; }

@script RequireComponent(AudioSource);

Thanks

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

1 Reply

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

Answer by Hrethric · Mar 14, 2013 at 07:39 PM

You have to give at least one of the objects a RigidBody, and then do this:

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnCollisionEnter.html

Depending on how you set up the logic, you will either need figure out what the car collided with, and decrease its health based on the result, or you will want to find what the TNT exploded, and do damage based on that.

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 namekaw · Mar 17, 2013 at 05:09 PM 0
Share

I have been trying this but cannot figure out correct syntax, have shown the script now, so if you know what I need to change that would be great.

avatar image Hrethric · Mar 29, 2013 at 10:19 PM 0
Share

Hey, sorry I didn't see that you responded to this. I'm not sure if you figured it out or not, but you want to send the message you your collider's gameObject. I haven't used the message system extensively in Unity, but to my understanding that's the way it works.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 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

Related Questions

Health Bar Help 2 Answers

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

Damage Indicator not showing correct position of attack. 0 Answers

Health Question 1 Answer

Damage script 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