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 Kergal · Aug 07, 2012 at 01:17 PM · c#referenceiddestruction

store contact with last before destruction

Hey guys, I will get straight to the topic. I have a tower shooting an arrow at an enemy - the enemy gets hit the arrow destructed. I want the tower to receive exp when the enemy gets destroyed. So far so easy ;) my problem - i have several towers shooting arrows at the enemy. I am storing the Towers ID in a variable attached to the arrow (GetInstanceID()) . How does the game realize which arrow hit the enemy the last(before destruction)? so that i can reference back to the according tower? my code is in c# . Any ideas? THanks! :) cheers Kergal

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Meltdown · Aug 07, 2012 at 01:26 PM

Add a variable to each enemy called LastHitById, which stores your TowerID that last hit.

When your enemy health is <= 0, stop accepting hits from arrows or the towers. This will make sure LastHitById has the correct value.

You can then create a public method on your Tower called AddXP, which you call from the enemy just before it dies.

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 Kergal · Aug 07, 2012 at 05:46 PM

hey Greg Quinn, that was fast :) . Thank you - that was exactly what I was looking for.

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 Meltdown · Aug 07, 2012 at 05:58 PM 0
Share

No problem. Please upvote and mark as answer so we know your question has been resolved. Also please don't post comments as 'answers'.

avatar image
0

Answer by Owen-Reynolds · Aug 07, 2012 at 03:18 PM

GQ's idea of remembering who hit you last is more versitile (esp if you can be poisoned, and die from it 2 seconds after being hit.)

But, the simplest way to "reference back to the according tower" is to store the tower's transform in the arrow (instead of the ID.) Then when you get a kill, use that to find and tell your owner. Ex (assuming your arrow does the work, not the player):

 Transform myTower; // set when arrow(me) is spawned

 OnCollisionEnter(Collision C)
   if(C is an enemy)
     enemyScript ES = C.transform.GetComponenent<enemyScript>();
     if(ES.dead==true) quit // rats -- overkilled a dying mob
 
     ES.takeDamage(); // even if it dies, will be around to end-of-frame

     // the new part:    
     if(ES.dead == true)
       if(myTower!=null) // did my owner die since it fired me?
         myTower.GetComponent<towerScript>.youKilled(C.transform);

Or, have the enemy "give" the credit: ES.takeDamage(myTower); and have the enemy's takeDamage check if the tower is still living and call getEXP for the tower.

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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Destroying Prefab Also Destroys Prefab Reference 1 Answer

Component passing self to method 1 Answer

How to reference another script and call a function in C# ? 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