Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Payaso_Prince · Jul 21, 2020 at 11:41 PM · instantiatebuttonsenablebutton trigger eventsbattle-system

Select an enemy to attack with buttons?

Hello!

I have an array of enemy objects and an Attack Button that executes Attack() on click. When I press my attack button, I would like another select button to appear above the first enemy in my enemy array and then when I press right or left it will go to the next enemy so that I can choose who to attack. I can't seem to come up with a way to implement this. My brain is fried over this, so any help would be GREATLY appreciated! :)

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
Best Answer

Answer by Dragondean · Jul 22, 2020 at 04:50 AM

I would assume that each enemy is part of an array. If so we create an int identifying which array piece is selected. so for instance:

 enemytype[] enemies;
 int selectedEnemy;

Then we use a keycode to detect input. If it is left, we check if there is an enemy one down the array, if not cycle it back up. Vice versa with the right side.

 if (Keycode.whateverkeyisleft){
      if (enemies[selectedEnemy-1] != null){
             selectedEnemy = enemies[selectedEnemy-1];
      } else {
             selectedEnemy = enemies[enemies.length-1];
      }
 }
 if (Keycode.whateverkeyisRight){
      if (enemies[selectedEnemy+1] != null){
             selectedEnemy = enemies[selectedEnemy+1];
      } else {
             selectedEnemy = enemies[0];
      }
 }

Then, by combining those two pieces of code, the selected enemy will change based upon input.

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 Payaso_Prince · Jul 22, 2020 at 11:57 AM 0
Share

That is a pretty good idea. But wouldn't it not work if you were on the left most enemy and press left? example: Enemy 0 > Press Left > Enemy -1 instead of the right most enemy? In other words, rather than going to the last element in the array, wouldn't it would go to -1 which doesn't exist? Similar situation with right.

Also, is there anyway to make the selector a UIButton? If possible, I'd like the button to start on enemy[0] and then move from enemy to enemy with left and right presses. When the selector UIButton is hovered an enemy, i'd like them to be the current selectedEnemy.

Thanks again for the response!

avatar image Dragondean Payaso_Prince · Jul 22, 2020 at 04:36 PM 0
Share

if you look closely, you will see that I did not use $$anonymous$$us 1 if left returned null. I took the array of enemies and then subtracted one to get the last element in the array. Look at the if else statement I put; it checks if there is an enemy to the left (the null statement) and then it runs different functions depending on if it was returned null, so as previously explained when it returns null it gets the array length and then subtracts one (you have to subtract one to get the last element).


With the ui button, you can create a public gameobject and then get the position of the enemy with selectedenemy.transform.position. Set the buttons position to that exact position. Then just add as much offset as you want with new Vector2(transform.position.x + offsetx, transform.position.y + offset y).


PS be sure your canvas is set to world space in render mode (see here for more info (https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/UICanvas.html))

avatar image
0

Answer by Payaso_Prince · Jul 22, 2020 at 02:43 AM

If it makes things clearer, here is what I am going for. alt text

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

159 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 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 remove action listeners from a button without actually clicking the button? 0 Answers

Making a clickable button in Editor Mode 0 Answers

Setting this Event trigger up to not do the function when trigger is up 0 Answers

[solved]how can i enable or disable a component of instantiated objects? 5 Answers

Checking if object intersects? 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