- Home /
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.
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?
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.
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).
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.
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.
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.
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?
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. :(
Your answer
![](https://koobas.hobune.stream/wayback/20220613150108im_/https://answers.unity.com/themes/thub/images/avi.jpg)