GetComponent

Hello,

I have been trying to make targeting script that get values from enemy health script and create target health bar ,

i got this error that i cant solve

Assets/My/Scripts/Targeting.cs(36,47): error CS1061: Type UnityEngine.GameObject' does not contain a definition for currentHealth’ and no extension method currentHealth' of type UnityEngine.GameObject’ could be found (are you missing a using directive or an assembly reference?)

Here’s my code

using UnityEngine;
using System.Collections;

public class Targeting : MonoBehaviour {
	public GameObject Target;
	
    public Ray ray;
	public RaycastHit hit;
	
	public int targetsCurrentHealth;
	public int targetsMaxHealth;
	
	public int barLength;
	
	public GUIStyle myStyle;
	
    void Update() {
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		
		if(Input.GetButtonDown("Fire1")) {
	        if(Physics.Raycast( ray, out hit, 100)) {
	            print("Hit something"); 
				
				if(hit.collider.gameObject.tag == "Enemy"){
					Target = hit.collider.gameObject;
					print ("Hit Enemy");
				}
				else {
					Target = null;
				}
			}
		}
		
		Target.GetComponent("EnemyHealth");
		
		targetsCurrentHealth = Target.currentHealth;
		
    }
	
	void OnGUI() {
		GUI.Box(new Rect(10, 10, barLength * 3, 25), targetsCurrentHealth + "/" + targetsMaxHealth, myStyle);
	}
	
	void ShowTargetsStats() {
		
	}
}

and here is enemyhealth script

using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour {
	
	public int currentHealth;
	public int maxHealth;
	
	public int barLength;
	
	public GUIStyle myStyle;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
	}

	void ApplyDamage(int damage) {
		currentHealth = currentHealth - damage;
	}
}

Can anybody tell me what im doing wrong?

Line 34. GetComponent returns a Component, so you need to assign that to a variable. Also, don’t use the string overload.

EnemyHealth enemyHealth = Target.GetComponent(EnemyHealth);
targetCurrentHealth = enemyHealth.currentHealth;

Thanks for reply,

when i use that code i get 3 new errors

Assets/My/Scripts/Targeting.cs(34,63): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected

Assets/My/Scripts/Targeting.cs(34,50): error CS1502: The best overloaded method match for `UnityEngine.GameObject.GetComponent(System.Type)’ has some invalid arguments

Assets/My/Scripts/Targeting.cs(34,50): error CS1503: Argument #1' cannot convert object’ expression to type `System.Type’

i tryed to change it to

EnemyHealth enemyHealth = Target.GetComponent("EnemyHealth");

targetCurrentHealth = enemyHealth.currentHealth;

but i get this error then

Assets/My/Scripts/Targeting.cs(34,29): error CS0266: Cannot implicitly convert type UnityEngine.Component' to EnemyHealth’. An explicit conversion exists (are you missing a cast?)

EnenmyHealth is not a String value. You do not need quotation marks.

yeah,but when i put it like this

EnemyHealth enemyHealth = Target.GetComponent(EnemyHealth);

targetCurrentHealth = enemyHealth.currentHealth;

i get three errors

Assets/My/Scripts/Targeting.cs(34,63): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected

Assets/My/Scripts/Targeting.cs(34,50): error CS1502: The best overloaded method match for `UnityEngine.GameObject.GetComponent(System.Type)’ has some invalid arguments

Assets/My/Scripts/Targeting.cs(34,50): error CS1503: Argument #1' cannot convert object’ expression to type `System.Type’

The string overload returns a Component object that you need to cast to the correct type. I always use the generic overload, so I may have typo’d my previous post.

EnemyHealth eh = Target.GetComponent("EnemyHealth") as EnemyHealth;
EnemyHealth eh = Target.GetComponent(typeof(EnemyHealth));  // I think this works?
EnemyHealth eh = Target.GetComponent<EnemyHealth>();  // I use this one.

Thanks ,first and third works :slight_smile:

Cheers