Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GetComponent

Discussion in 'Scripting' started by Dino1997, Sep 6, 2012.

  1. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    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
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Targeting : MonoBehaviour {
    6.     public GameObject Target;
    7.    
    8.     public Ray ray;
    9.     public RaycastHit hit;
    10.    
    11.     public int targetsCurrentHealth;
    12.     public int targetsMaxHealth;
    13.    
    14.     public int barLength;
    15.    
    16.     public GUIStyle myStyle;
    17.    
    18.     void Update() {
    19.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    20.        
    21.         if(Input.GetButtonDown("Fire1")) {
    22.             if(Physics.Raycast( ray, out hit, 100)) {
    23.                 print("Hit something");
    24.                
    25.                 if(hit.collider.gameObject.tag == "Enemy"){
    26.                     Target = hit.collider.gameObject;
    27.                     print ("Hit Enemy");
    28.                 }
    29.                 else {
    30.                     Target = null;
    31.                 }
    32.             }
    33.         }
    34.        
    35.         Target.GetComponent("EnemyHealth");
    36.        
    37.         targetsCurrentHealth = Target.currentHealth;
    38.        
    39.     }
    40.    
    41.     void OnGUI() {
    42.         GUI.Box(new Rect(10, 10, barLength * 3, 25), targetsCurrentHealth + "/" + targetsMaxHealth, myStyle);
    43.     }
    44.    
    45.     void ShowTargetsStats() {
    46.        
    47.     }
    48. }
    49.  
    and here is enemyhealth script

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class EnemyHealth : MonoBehaviour {
    6.    
    7.     public int currentHealth;
    8.     public int maxHealth;
    9.    
    10.     public int barLength;
    11.    
    12.     public GUIStyle myStyle;
    13.    
    14.     // Use this for initialization
    15.     void Start () {
    16.    
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
    22.     }
    23.  
    24.     void ApplyDamage(int damage) {
    25.         currentHealth = currentHealth - damage;
    26.     }
    27. }
    28.  
    29.    
    30.    
    31.  
    32.  
    Can anybody tell me what im doing wrong?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Line 34. GetComponent returns a Component, so you need to assign that to a variable. Also, don't use the string overload.

    Code (csharp):
    1.  
    2. EnemyHealth enemyHealth = Target.GetComponent(EnemyHealth);
    3. targetCurrentHealth = enemyHealth.currentHealth;
    4.  
     
  3. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    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

    Code (csharp):
    1.  
    2. EnemyHealth enemyHealth = Target.GetComponent("EnemyHealth");
    3.  
    4. targetCurrentHealth = enemyHealth.currentHealth;
    5.  
    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?)
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    EnenmyHealth is not a String value. You do not need quotation marks.
     
  5. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    yeah,but when i put it like this
    Code (csharp):
    1.  
    2. EnemyHealth enemyHealth = Target.GetComponent(EnemyHealth);
    3.  
    4. targetCurrentHealth = enemyHealth.currentHealth;
    5.  
    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'
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.

    Code (csharp):
    1.  
    2. EnemyHealth eh = Target.GetComponent("EnemyHealth") as EnemyHealth;
    3. EnemyHealth eh = Target.GetComponent(typeof(EnemyHealth));  // I think this works?
    4. EnemyHealth eh = Target.GetComponent<EnemyHealth>();  // I use this one.
    5.  
     
  7. Dino1997

    Dino1997

    Joined:
    Nov 8, 2010
    Posts:
    30
    Thanks ,first and third works :)

    Cheers