• 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
Question by JoshMBeyer · Mar 24, 2014 at 02:58 PM · arrayworldtoscreenpointthroughcycletargets

How to cycle through targets?

I am trying to make it to where when TAB is pressed, target is equal to targets[0], then targets[1], targets[2] (However long the array is) then repeat back from 0.

And also having trouble showing a texture icon to show which one is targeted currently. It shows up but farrrrr from the actual target.

here is what i have so far.

 using UnityEngine;
 using System.Collections;
 
 public class T1_TargetSystem : MonoBehaviour
 {
     public GameObject[] targets;
    
     private int currentTarget;
     private int totalTargets;
 
     public Transform target;
     public Texture2D image;
 
     Vector3 point;
     void Start()
     {
         
     }
 
     void Update()
     {
         target = targets[currentTarget];
         if (currentTarget == targets.Length)
         {
             currentTarget = 0;
         }
         targets = GameObject.FindGameObjectsWithTag("Enemy");
         if (currentTarget == totalTargets)
         {
             return;
         }
         else
         {
             // Find screen position for target
             point = Camera.main.WorldToScreenPoint(target.position);
             if (Input.GetKeyDown(KeyCode.Tab))
             {
                 currentTarget++;
             }
         }
     }
 
     void OnGUI()
     {
         Rect rect = new Rect(point.x, point.y, 20, 20);
     }
 }
Comment

People who like this

0 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 vexe · Mar 24, 2014 at 03:08 PM 0
Share

For cycling:

 if (Input.GetKeyDown(KeyCode.Tab))
 {
     currentTarget = (currentTarget + 1) % targets.Length;
 }
avatar image JoshMBeyer · Mar 24, 2014 at 06:15 PM 0
Share

I am having an error with targets[](GameObject) to target(Transform)

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by SirCrazyNugget · Mar 24, 2014 at 03:12 PM

 //only create the array once (or when new targets are added)
 void Start(){
    targets = GameObject.FindGameObjectsWithTag("Enemy");
 }
 
 void Update(){
    if(Input.GetKeyDown(KeyCode.Tab)){
       currentTarget = currentTarget + 1 % targets.Length;
       target = targets[currentTarget];
    }
 }

Why not just place an object in the 3D space above the intended target rather than trying to convert world space to screen space?

Comment

People who like this

0 Show 4 · 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 JoshMBeyer · Mar 24, 2014 at 06:14 PM 0
Share

I was wanting some sort of visual icon over the targets. So if they are behind a wall or hill I can still see their target icon such as a red triangle image around them if they are targeted

avatar image JoshMBeyer · Mar 24, 2014 at 06:16 PM 0
Share

cannot complicity convert unityengine.gameobject to unityengine.transform

avatar image FinKone · Mar 24, 2014 at 06:18 PM 0
Share

Yea hes saying you could attach whatever target icon to the actual object and enanle/disable and even rotate them towards the players if needed rather then figuring out a world space thing.

avatar image JoshMBeyer · Mar 24, 2014 at 06:33 PM 0
Share

oh okay alot better idea. But still having issues getting the cycle through targets to work

avatar image

Answer by JoshMBeyer · Mar 24, 2014 at 06:28 PM

I got it to get rid of errors buy adding this but its not doing anything at all. no throws to console or anything

 if (Input.GetKeyDown(KeyCode.Tab))
             {
                 currentTarget = currentTarget + 1 % targets.Length;
                 target = targets[currentTarget].transform;
             }
Comment

People who like this

0 Show 4 · 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 JoshMBeyer · Mar 24, 2014 at 06:29 PM 0
Share

saying t1_targetsystem.totaltargets is never assigned to and will always have value of 0??

avatar image JoshMBeyer · Mar 24, 2014 at 06:31 PM 0
Share

Added this but same results, no errors but still doesn't do anything.

 void Start()
     {
         totalTargets = targets.Length;
     }
avatar image Destran · Mar 24, 2014 at 06:38 PM 1
Share

http://www.youtube.com/watch?v=zNuCvWnGdXA

Telling you bro, this is almost exactly what you want targeting wise minus the texture icon.

avatar image JoshMBeyer · Mar 24, 2014 at 06:49 PM 0
Share

thanks man. that worked perfectly

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Get list of nearby targets (JS) 1 Answer

Projectile Selection through Array 1 Answer

Array Help 2 Answers

cycle trhu an Array of gameobjects? 1 Answer

Make a Button For Every String in an Array of Level Names 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