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 marius_87 · May 28, 2014 at 03:12 PM · c#targetfindgameobjectswithtag

c# Get target gameobject from a list

Hi all, first time here and just begin learning scripting. :)

I have a script which is instantiated (many times) who need a target (transform). I tried the code below but makes all scripts use the same target (the first one found).

 public GameObject target;   

 // Use this for initialization
 void Start () {
 
     target = GameObject.FindGameObjectsWithTag ("Pivot");
 }

I also tried using a list but i don't know how to get another target to each script (it keep choosing the first target).

Another test script with this error (error CS1579: foreach statement cannot operate on variables of type UnityEngine.GameObject' because it does not contain a definition for GetEnumerator' or is not accessible) :

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class manyTargets : MonoBehaviour 
 {
         
         public GameObject target;
         public List<GameObject> pivotTarget;
         
         void Start ()
         {
             
             if (target != null)
             {
                 foreach(GameObject go in target)
                 {
                     pivotTarget.Add(go);
                 }
             }
         }
 }

I have try from yesterday to make it work with no luck. I hope i can get some help because i'm out of ideas. If i'm not to clear, sorry (english is not my first language :) ). Thanks a lot.

Comment
Add comment · Show 3
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 cryingwolf85 · May 28, 2014 at 03:20 PM 0
Share

Is your "target" the object that is using the script? Or is it a reference to another object? What are you attaching this script to?

avatar image _dns_ · May 28, 2014 at 03:24 PM 0
Share

Hi, your object target needs to be a table: a "GameObject[]" and not a GameObject. Well, that's only a hint for the CS1579 error, it should compile but I'm not sure it'll do what you want.

avatar image marius_87 · May 28, 2014 at 03:26 PM 0
Share

No, script is on a prefab with an health bar, "target" should be an empty game object above enemy head (tagged with "Pivot"). So for each enemy i have to instantiate "health bar" and move it to "target" position (Pivot position).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cryingwolf85 · May 28, 2014 at 03:29 PM

If I understand your question correctly, it seems that a static variable may be of good use here. Basically they remain the same wherever your reference them. Create a class like this:

 public class Info {
     
     public static GameObject target;
 
 }

and when you need to set a target, use this line in another script:

 Info.target = new GameObject(); // Or whatever game object you want the target to be

Doing something with the target (In this example setting position)

 transform.position = Info.target.transform.position;

Use this with the first type of script you created. No need for an array. I see people use arrays in so many situations where they are completely unnecessary.

Comment
Add comment · Show 6 · 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 marius_87 · May 28, 2014 at 04:08 PM 0
Share

Thanks. Now i try to learn about statics (never try them before) and next step is to try your script, i hope this time will work. Thanks again.

avatar image cryingwolf85 · May 28, 2014 at 07:56 PM 0
Share

No problem.

avatar image marius_87 · May 29, 2014 at 10:21 AM 0
Share

I guess that too much c# for me,look simple but... can't get it working. So i try other method. On the enemy scrip (in this example "scriptTester") that instantiate the hud prefab i'm trying to create an array (i know, i'm back to arrays :D ) with the hud prefabs and then search for the "Target" to see if it's null or not. If it's null assign "scriptTester" position(position of the object with this script attached). If !=null search for next null in array and assign "scriptTeste"position. So each "scriptTester" must set his position to an "eHUD"

This is my script so far:

 using UnityEngine;
 using System.Collections;
 
 public class HUDin : $$anonymous$$onoBehaviour {
 
     //public GameObject HUDprefab;
     public GameObject hudText;
     public GameObject parent;
 
     public GameObject scriptTester;
     public GameObject[] eHUD;
 
     // Use this for initialization
     void Start () {
 
 
         Component UItarget = eHUD [0].GetComponent<UIFollow> ().target;   // error
 
         //Instantiate(HUDprefab)
         hudText = NGUITools.AddChild (parent, hudText);
 
 
         // Apply target
         eHUD = GameObject.FindGameObjectsWithTag ("EnemyHUD");
 
         foreach (GameObject enemyHUD in eHUD) 
         {
             if (UItarget == null) {
                 UItarget = scriptTester.transform;   // error
             }
 
             if (UItarget != null){
 
                 // here i don't know how to make it look forward in array until it find an empty "target"
                 }
 
         }

I hope i'm clear :) .Thanks again.

avatar image cryingwolf85 · May 29, 2014 at 05:59 PM 0
Share

Okay so tell me if i'm wrong:

You are trying to get a health bar above an enemies head, each with their own unique health?

avatar image marius_87 · May 29, 2014 at 06:22 PM 0
Share

Yes. I use the HUDText from assets store. I have a prefab with a label and a health bar. This prefab has a script to follow a target (enemy in my case). When I instantiate an enemy i have to instantiate HUD prefab and add enemy as target (or an empty game object parented to enemy). The problem is assigning the target to the Follow script. Each enemy has an empty game object named Pivot, this have to be the target. I think is something simple but ... not for me. :(

Show more comments

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Detect clone using FindGameObjectsWithTag method. 1 Answer

Look at object when it is in the range of the player 0 Answers

GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 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