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 AndrewRyan · Nov 26, 2014 at 07:15 AM · gameobjectarrayfor-loop

Find objects with tag?

Hi,

I made a ui script that instantiates a temporary guitext on an object that receives damage. This works with individual objects, but I want it so that for all gameobjects in the scene with specific tags, a guitext is instantiated above them, at their current position. How would I go about this? I attempted it but with no luck.

Here is my current script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.Linq;
 
 public class HitPoints : MonoBehaviour {
 
     //the next position to instantiate a point prefab(at the next harmed enemy)
     private Vector2 nextPosition;
 
     //change the UI canvas transform
     public Canvas prefab;
 
     //access the UI text component
     public Text text;
 
     //how much damage to print
     private int _damage;
 
     //an array of enemies, their damage and health
     private GameObject[] enemies;
     private Transform[] _enemyPositions;
     private Health[] _enemyHealth;
     private int[] _enemyDamage;
 
     //offset points from entity center
     public Vector3 offset = new Vector3(0f, 1.5f, 0f);
     
     void Awake () {
 
         //get all enemies in the scene and their health and damage values
         enemies = GameObject.FindGameObjectsWithTag ("Enemy");
 
         //get the health, damage of their weapon, and position and store into array
         for (int i = 0; i < enemies.Length; i++)
         {
             _enemyHealth[i] = enemies[i].GetComponent<Health>();
             _enemyDamage[i] = enemies[i].GetComponentInChildren<EnemyAttack>().damage;
             _enemyPositions[i] = enemies[i].GetComponent<Transform>();
             print(enemies.Length);
         }
     }
 
     void FixedUpdate () {
 
         //as long as there are enemies, execute
         if(enemies.Length > 0)
         {
             print ("there are " + enemies.Length + " enemies.");
 
             //for every enemy
             for(int i = 0; i < enemies.Length; i++)
             {
                 //instantiate a point prefab for every hurt enemy
                 if(_enemyHealth[i].hurt)
                 {
                     nextPosition = _enemyPositions[i].position + offset;
                     text.text = _damage.ToString();
                     //print ("got this far");
                     Canvas hitPoint = Instantiate(prefab, nextPosition, transform.rotation) as Canvas;
                 }
             }
         }
     }
 
     //get damage from weapons
     public void Damage(int dmg)
     {
         _damage = dmg;
     }
 }
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
1
Best Answer

Answer by fafase · Nov 26, 2014 at 07:18 AM

Instead of creating canvas above their head, I would rather add it to the prefab of the object already positioned but inactive. Then you can just show the GUI:

 public void ShowUI()
 {
    var objects = GameObject.FindGameObjectsWithTag("Tag");
    foreach(GameObject o in objects){
       var comp = o.transform.Find("Canvas");
       comp.gameObject.SetActive(true);
 
    }
 }

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 AndrewRyan · Nov 26, 2014 at 09:32 AM 0
Share

Wow, I really over-thought this. Thanks for the help.

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

2 People are following this question.

avatar image avatar image

Related Questions

how to check for GameObject is null in array with random 1 Answer

(20,57): BCE0022: Cannot convert 'EnemyAI[]' to 'EnemyAI'. 1 Answer

Use an objects (from array) position to focus a camera on 3 Answers

Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere 3 Answers

Detection if a GameObject is below you or next to you? 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