• 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 /
  • Help Room /
avatar image
0
Question by Wimmydittle · Nov 20, 2015 at 08:33 AM · scripting beginnerontriggerenter

OnTriggerEnter find damage that other object should deal C#

Hello. I have different space ships that will eventually have different shots with different amounts of damage, as well as obstacles that will cause damage when they hit the ships. My question is, how do I get a ship to find out what the damage of the object that hit it with OnTriggerEnter is, and then subtract that amount from the ship's health?

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

Answer by KdRWaylander · Nov 20, 2015 at 02:52 PM

If i understand correctly what you say, you want different objects to deal different amounts of damage to the ship ?

If you have object A and B, ship S:

  • First solution would be a script on your objects, when a collider hits the collider of your object, if the tag of the object that has the collider is "Ship" → deal xx damage. You write one script for each kind of object or a single script for all objects and when the ship hits the collider you decide how much damage you deal with a swtch case on the name of the object.

  • Second solution would be the reverse situation, you have a script on your ship, if it hits a collider, it checks the tag of the object that bears the collider, if this tag is "object A", substract x health point to the ship, if the tag is "object B", ...

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 Wimmydittle · Nov 22, 2015 at 08:48 PM 0
Share

Yes. That is exactly what I wanted. Thank you Waylander.

avatar image
0

Answer by Wimmydittle · Nov 22, 2015 at 09:59 PM

Now that I think about it, I will probably eventually want to have upgrades on the ships, and I would really rather not make a bunch of different ship that are the exact same except for the tags. Any answers? I was thinking about the possibility of accessing a script that all ships have, and a variable (like CurrentHealth or something) that all ships have through GetComponent, and that every time someone starts the game, their ship automatically adjusts to their upgrades. How would accessing a script through GetComponent from an object that triggered a collider work?

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 Wimmydittle · Nov 22, 2015 at 10:07 PM 0
Share

What if I do something like the following, where every game object has a script called AccessibleScript and a variable called Damage?

void OnTriggerEnter(Collider other) { float Damage = other.gameObject.GetComponent().Damage; CurrentHealth =- Damage; }

avatar image Wimmydittle · Nov 22, 2015 at 10:13 PM 0
Share

I just checked the script and it worked! Thanks for your help!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I check if an object has entered a trigger with a script attached to a different object? 1 Answer

Unity Multiplayer Disconnection 0 Answers

Holding down UI mobile button 4 Answers

Really Really Need Help With Script 2 Answers

How would I restart a scene by clicking on something? 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