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 jimjames · Jan 13, 2015 at 12:39 AM · targets

Targets in Collider

Dear: Readers

I have this problem that has been bothering me for awhile now. Want to ask you all to see if you can help me solve it, with another perspective.

I am designing a TD game, and having problems with the que system. Right now I have it set up, as a enemy comes in the turret collider, the turret adds 1 to "counter" variable and when exits take 1 away from "counter". When "counter

The problem lays in the "counter" with enter and exit. As when a enemy gets destroyed inside a collider the turret does not register it as a exit.

Hope someone can help out with the problem, maybe even come at it with a totally new perspective.

Thank You: James

Comment
Add comment · Show 4
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 bodec · Jan 13, 2015 at 12:55 AM 0
Share

please supply the code describing the script doesn't help when it may be a simple miss type.

avatar image InvincibleCat · Jan 13, 2015 at 12:59 AM 0
Share

No he is right, when you instantiate an object inside a collider OnTriggerEnter won't be rised and if you destroy an object that's inside a collider, OnTriggerExit won't be rised too

But my solution still works ;)

avatar image jimjames · Jan 13, 2015 at 01:19 AM 0
Share

Sorry but if you do instantiate a object inside a collider OnTriggerEnter does register it. But not on exit.

avatar image InvincibleCat · Jan 13, 2015 at 01:22 AM 0
Share

Yes, it's true, my bad. Did you try my solution?

2 Replies

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

Answer by InvincibleCat · Jan 13, 2015 at 12:55 AM

You have multiple solutions. I will give you an easy one to understand even if it's not the best one.

Add List or turrets in your enemies script. OnTriggerEnter, you add the turret to the enemy list.

Then, OnDestroy (in your enemy script) loop on your turrets to notify that the enemy has been killed

the other solution is to have something like

 public Action<EnemyScript> OnBeKilled = null;
 private void OnDestroy()
 {
      if(OnBeDestroy != null)
      {
          OnBeDestroy(this);
      }
 }

On your EnemyScript. (Off course, replace EnemyScript by your Enemy class).

And then, in your Turret:

 private void OnTriggerEnter(Collider pCollider)
 {
     EnemyScript enemy = pCollider.GetComponent<EnemyScript>();
     enemy.OnBeKilled += OnEnemyBeKilled;
     //replace with the real name of your variable
     enemyCounter++;
 }
 
 private void OnTriggerExit(Collider pCollider)
 {
     EnemyScript enemy = pCollider.GetComponent<EnemyScript>();
     enemy.OnBeKilled -= OnEnemyBeKilled;
     //replace with the real name of your variable
     enemyCounter--;
 }

 private void OnEnemyBeKilled()
 {
     //replace with the real name of your variable
     enemyCounter--;
 }
Comment
Add comment · Show 7 · 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 jimjames · Jan 13, 2015 at 01:04 AM 0
Share

Yes i have thought of that but ins$$anonymous$$d of Add List on enemies I tried it on my turrets. But eachtime I looped the list looking for the next target it really reduced my frames. Is there a reason why? or maybe it was bad coding on my behalf?

avatar image InvincibleCat · Jan 13, 2015 at 01:07 AM 0
Share

that's why you need the list on your enemies. You only need to change something onDestroy.

With your solution, you loop on stuff each frame even if you don't need to. It your enemy that should notify the turrets

avatar image jimjames · Jan 13, 2015 at 01:07 AM 0
Share

Also was wonder about the other solutions you have.

avatar image InvincibleCat · Jan 13, 2015 at 01:15 AM 0
Share

I Update my answer

avatar image jimjames · Jan 13, 2015 at 01:26 AM 0
Share

I understand what you are trying to do here. I will give it a shoot and come back.

Show more comments
avatar image
0

Answer by DFortun81 · Jan 13, 2015 at 01:13 AM

You can solve this by using a semi-complicated method called Event Handlers. Essentially, you would create a simple Delegate and event handling system in your enemy's base class (if it doesn't have one, make one - these are especially useful!) and then have objects that are dependent on specific non-Unity events receive notifications when certain things happen.

 public class UnitBase : MonoBehaviour
 {
     // Declare our delegate type. Functions that register
     // with events of this type much also take a 'unit' argument.
     public delegate void UnitDelegate(UnitBase unit);
 
     // This event acts as a callback for objects that need
     // to know when this unit dies and is destroyed.
     public event UnitDelegate OnUnitDestroyed;
 
     // OnDestroy is a standard MonoBehaviour function and gets
     // called automatically when an object is freed from memory
     // we will use this as our Invocation function.
     public void OnDestroy()
     {
         if(OnUnitDestroyed != null) OnUnitDestroyed(this);
     }
 }

Then in your Turret code, you would simply register for the OnUnitDestroyed event in the code portion where you are performing counter + 1.

 UnitBase unit = currentTarget.GetComponent<UnitBase>();
 if(unit != null) {
    // To prevent this from getting called multiple times,
    // it is a good idea to Unregister immediately before
    // registering when utilizing events outside of Awake.
    unit.OnUnitDestroyed -= HandleOnUnitDestroyed;
    unit.OnUnitDestroyed += HandleOnUnitDestroyed;
 }

And then create an event handler function that does this:

 private void HandleOnUnitDestroyed(UnitBase unit)
 {
     // The unit was destroyed, so unregister!
     if(unit != null) unit.OnUnitDestroyed -= HandleOnUnitDestroyed;
     --counter;
 }

Then in the place where you normally decrement your counter variable, call the handle function instead:

 // Substitute the word "currentTarget" with whatever object or component you
 // have access to in the function you're using for this.
 HandleOnUnitDestroyed(currentTarget.GetComponent<UnitBase>());

This makes it so that any class derived from "UnitBase" has its destruction event unregistered when its supposed to as well as process your decrementer.

Comment
Add comment · Show 11 · 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 jimjames · Jan 13, 2015 at 01:27 AM 0
Share

hmmm. I have not worked with Event Handlers yet. Will have to investigate this method more.

avatar image InvincibleCat · Jan 13, 2015 at 01:30 AM 0
Share

In fact, it's almost the same solution u_u. I prefer $$anonymous$$e just because you don't need to declare any delegates. Action is no more than delegate $$anonymous$$yDelegate(T value)

avatar image DFortun81 · Jan 13, 2015 at 01:33 AM 0
Share

Correct, InvincibleCat. I posted it a few $$anonymous$$utes after yours. I just type slower. I prefer the standard delegate/event notation since I came from a purely C# background when I started with Unity, although I suppose both methods essentially do the same thing under the hood.

To each their own.

avatar image InvincibleCat · Jan 13, 2015 at 01:36 AM 0
Share

InvinCible Cat not InvinSible Cat ^^ Yes I know it's the same. I come from pure C# too but I think it's just a personal stuff :) I used delegates a lot but then I prefer Action in some situations.

avatar image InvincibleCat · Jan 13, 2015 at 01:42 AM 0
Share

And I thought that using event is better that way:

 private UnitDelegate _onUnitDestroyed = null;
 public event UnitDelegate OnUnitDestroyed
 {
     add { _onUnitDestroyed += value; }
     remove { _onUnitDestroyed -= value}
 }

To prevent any subscriber using "=" and unsubscribe all the others

Show more comments

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

28 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 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

Will Unity support morph targets built in natively? 3 Answers

Semi-Controlled Array Randomization 1 Answer

Tell if navmeshagent has reached target? 0 Answers

Making a target move when hit 3 Answers

Get list of nearby targets (JS) 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