• 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 24, 2017 at 04:50 AM · instancevalueboolstore

How do I store bool values for each instance of an object and then access them per frame?

I'm making a Tower Defense game. My Towers/Turrets currently know if they can shoot an enemy if the enemy triggers OnTriggerEnter(). I then code it so it focuses that enemy until it the enemy dies.

The problem comes in when the target dies. Because I'm triggering off of OnTriggerEnter() to start shooting, the other enemies entered range long ago and don't trigger it anymore. They never get shot at. My solution is to go through a list of bools for each enemy per frame (or five) and check if their inrange = true and fire if so. inrange will be set by OnTriggerEnter().

How do I store the OnTriggerEnter() bool on each instance, and then access those every frameto determine if there is something left to shoot? I'm doing it this way because I think accessing bools is much more efficient than checking to see which enemy is nearest every frame (or five). Please correct me if I'm wrong and I'll do it the easy way.

 public class Turret : MonoBehaviour {
 
     public GameObject Bulletprefab;
     public float rotationspeed = 35;
     bool inrange = false;
     GameObject targetplayer;
     float attackrate = 0.5f;
     float nextattack = 0;
     public int attackstr = 10;
     GameObject lockedon;
 
     // Update is called once per frame
     void Update()
     {
         // Rotate turret fancy-like
         transform.Rotate(Vector3.up * Time.deltaTime * rotationspeed, Space.World);
 
         // Find cloest player and lockon until it's dead, if it isn't already lockedon to another player
         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?*****
                 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 · Show 2
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 hexagonius · Feb 24, 2017 at 10:33 AM 1
Share

unless you have rapid fire turrets you'd only need to check distances every shot. and if the enemies circuit your turret getting closer later, the bools would lie.
nevertheless, you can use a Dictionary to store the pairs. but I'd just store the enemies in a list

Show more comments

2 Replies

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

Answer by fgbg · Feb 26, 2017 at 04:19 AM

Answered here: http://answers.unity3d.com/questions/1318532/how-do-i-store-a-list-on-a-unit-consisting-of-all.html

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
1

Answer by Tourist · Feb 24, 2017 at 11:10 AM

If you check the squared distance instead of the distance, it should be enough in term of optimization. But yeah you could make a trigger collider and keep a list of all enemies that have entered (and not left yet) the trigger volume.

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 fgbg · Feb 25, 2017 at 07:06 PM 0
Share

Ok yeah, so how do I actually code that? I'm familiar with arrays and iterating with loops, but not with storing the specific value of and instance of an object and then also accessing it.

See lines 30, 40 and 53 above.

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

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

How do I check if bool is true from another script? 2 Answers

Changing a certain element from a bool array in another script 1 Answer

Downloading of the draft project version 0 Answers

Problems with armature in Blender animation 0 Answers

Several questions about Class in Unity 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