Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by fgbg · Feb 25, 2017 at 07:26 PM · variableontriggerenterinstancelockon

How do I store a list in an object consisting of ALL towers that it is in range of, and access that list from the towers?

I want to know which towers my little dudes have triggered OnTriggerEnter() for and save them in a little list (on the instance itself). I then want to use this data so my towers know if they can shoot said unit or not.

I'm having an issue currently where my towers lock on and fire onto a unit, but when he moves out of range he's still being shot because other instances of that unit are stiller triggering OnTriggerEvent/OnTriggerStay... globally?

Can someone show me how to code this? Also, if this isn't the best way to do this I'd be more than happy to hear other ways to code it.

Turret code follows. Check out lines 15, 25 and 38:

 void Update()
     {
         // Find cloest player and lockon until it's dead, if it isn't already lockedon to another player or  leaves range
         if (!lockedon)
         {
             targetplayer = FindClosestPlayer();
             lockedon = targetplayer;
         }
         // If lockedon to a target, attack
         if (lockedon)
         {
             // If player is in range (OnTriggerEnter), attack
             if (inrange)
             {
                 // *****HOW DO I CHECK IF targetplayer's inrange VARIABLE == TRUE HERE INSTEAD?*****
                 Attack();
             }
         }
     }
 
     void OnTriggerStay(Collider co)
     {
         if (co.tag =="Player")
         {
             //***** HOW DO I SET THE SPECIFIC PLAYER INSTANCE inrange VARIABLE TO TRUE HERE INSTEAD?*****//
             inrange = true;
             Debug.Log(targetplayer);
         }
         
     }
 
     void OnTriggerExit(Collider co)
     {
         // If it uncollides with an Enemy, set inrange false
         if (co.tag == "Player")
         {
             //***** HOW DO I SET THE SPECIFIC PLAYER INSTANCE inrange VARIABLE TO False HERE INSTEAD?*****//
             inrange = false;
             Debug.Log("Uncollided with:");
             Debug.Log(co.tag);
         }
     }

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 fgbg · Feb 25, 2017 at 09:12 PM

I fixed this after a bit of a headache. I now add or remove the gameobjects from the unitsinrange list when they trigger Enter or Exit. This is how I'll know if a unit is in range (and can therfore be shot). The tower's reference the list before attacking:

 void Update()
 {
     // Find cloest player, lock on and attack until it's dead, as long as it is in range
     Debug.Log("Does target player exist?" + targetplayer);
     if (!targetplayer)
     {
         targetplayer = FindClosestPlayer();
         Debug.Log("Closest target is: " + targetplayer);
     }
     // If locked on to a target, allow attack
     else  if(targetplayer)
     {
         // If player is in range, attack
         Debug.Log("Is target in range? " + unitsinrange.Contains(targetplayer));
         if (unitsinrange.Contains(targetplayer))
         {
             Debug.Log("Attacking: " + targetplayer);
             Attack(targetplayer);
         }
         // If closest target wasn't in range in range, break the lock on
         else
         {
             targetplayer = null;
         }
     }
 }


 void OnTriggerEnter(Collider co)
 {
     if (co.tag =="Player")
     {
         Debug.Log("Turret added this to list: " + co.gameObject);
         unitsinrange.Add(co.gameObject);
         foreach (GameObject unit in unitsinrange)
         {
             Debug.Log("Contents of list: " + unit);
         }
     }
     
 }


 void OnTriggerExit(Collider co)
 {
     if (co.tag == "Player")
     {
         unitsinrange.Remove(co.gameObject);
         Debug.Log("Turret removed this from list: " + co.gameObject);
         foreach (GameObject unit in unitsinrange)
         {
             Debug.Log("Contents of list: " + unit);
         }
     }
 }
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

95 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Access variable from different class 1 Answer

How do I specify an instance as a listener to an event trigger component? 0 Answers

How can I access material of a mesh renderer without receiving and instance of it? 1 Answer

Setting OnTriggerEnter to a second object collider 2 Answers

Collission is detected very late. onTriggerEnter is working very late (i.e 5 Seconds late) 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