Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

"Are you missing an assembly reference?"

Discussion in 'Scripting' started by sgtpotato3, Jun 22, 2018.

  1. sgtpotato3

    sgtpotato3

    Joined:
    Jun 21, 2018
    Posts:
    3
    Here is the error message in full: Assets/Scripts/HurtPlayer.cs(13,67): error CS1061: Type `PlayerHealthManager[]' does not contain a definition for `HurtPlayer' and no extension method `HurtPlayer' of type `PlayerHealthManager[]' could be found. Are you missing an assembly reference?

    Here are the two important scripts:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HurtPlayer : MonoBehaviour
    6. {
    7.     public int damageToGive;
    8.  
    9.     public void OnTriggerEnter(Collider other)
    10.     {
    11.         if(other.gameObject.tag == "Player")
    12.         {
    13.             other.gameObject.GetComponents<PlayerHealthManager>().HurtPlayer(damageToGive);
    14.         }
    15.     }
    16. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerHealthManager : MonoBehaviour {
    6.  
    7.     public int startingHealth;
    8.     public int currentHealth;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         currentHealth = startingHealth;
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         if(currentHealth <= 0)
    18.         {
    19.             gameObject.SetActive(false);
    20.         }
    21.  
    22.     }
    23.  
    24.     public void HurtPlayer(int damageAmount)
    25.     {
    26.         currentHealth -= damageAmount;
    27.     }
    28. }
    29.  
    If you need any more info just let me know. Im trying to fix the error if you cant already tell. Thanks.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    I think you just want `GetComponent` instead of `GetComponents`. You're trying to get an array of PlayerHealthManager objects the way you have it now, instead of the single one that probably exists on the object.