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. Dismiss Notice

Question Can Someone tell me what this error is?

Discussion in 'Scripting' started by TrexMatrix, Aug 9, 2023.

Thread Status:
Not open for further replies.
  1. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    I got this error and I don't what it is, I tried googling it and still don't understand it.

    Assets\Scripts\Gun.cs(49,32): error CS1061: 'IDamageable' does not contain a definition for 'TakeDamage' and no accessible extension method 'TakeDamage' accepting a first argument of type 'IDamageable' could be found (are you missing a using directive or an assembly reference?)

    This is my script too:

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4.  
    5. public class Gun : MonoBehaviour
    6. {
    7.     [Header("References")]
    8.     [SerializeField] private GunData gunData;
    9.     [SerializeField] private Transform muzzle;
    10.  
    11.     float timeSinceLastShot;
    12.  
    13.     private void Start()
    14.     {
    15.         PlayerShoot.shootInput += Shoot;
    16.         PlayerShoot.reloadInput += StartReload;
    17.     }
    18.  
    19.     public void StartReload()
    20.     {
    21.         if (!gunData.reloading)
    22.         {
    23.             StartCoroutine(Reload());
    24.         }
    25.     }
    26.  
    27.     private IEnumerator Reload()
    28.     {
    29.         gunData.reloading = true;
    30.  
    31.         yield return new WaitForSeconds(gunData.reloadTime);
    32.  
    33.         gunData.currentAmmo = gunData.magSize;
    34.  
    35.         gunData.reloading = false;
    36.     }
    37.  
    38.     private bool CanShoot() => !gunData.reloading && timeSinceLastShot > 1f / (gunData.fireRate / 60f);
    39.  
    40.     public void Shoot()
    41.     {
    42.         if (gunData.currentAmmo > 0)
    43.         {
    44.             if (CanShoot())
    45.             {
    46.                 if (Physics.Raycast(muzzle.position, transform.forward, out RaycastHit hitInfo, gunData.maxDistance))
    47.                 {
    48.                     IDamageable damageable = hitInfo.transform.GetComponent<IDamageable>();
    49.                     damageable?.TakeDamage(gunData.damage);
    50.                 }
    51.  
    52.                 gunData.currentAmmo--;
    53.                 timeSinceLastShot = 0;
    54.                 OnGunShot();
    55.             }
    56.         }
    57.     }
    58.  
    59.     private void Update()
    60.     {
    61.         timeSinceLastShot += Time.deltaTime;
    62.  
    63.         Debug.DrawRay(muzzle.position, muzzle.forward);
    64.     }
    65.  
    66.     private void OnGunShot() {
    67.         throw new NotImplementedException();
    68.     }
    69.  
    70. }
    71.  
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    in
    Code (CSharp):
    1. IDamageable damageable = hitInfo.transform.GetComponent<IDamageable>();
    you have selected a type of IDamageable, it does not grant you access to TakeDamage even if there is one, its not part of that interface
     
  3. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    so how would I fix it?
     
  4. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    569
    Is that exact method, with that exact argument, defined in IDamageable? If not, then you should add it to the interface contract. If you define it in some other class that inherets IDamageable, then you cannot cast to the interface and still have access to the method. Make sure to define that method in the interface.
     
  5. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    So would this work?

    Code (CSharp):
    1.  
    2. public interface IDamageable
    3. {
    4.        int CurrentHealth { get; }
    5.  
    6.        void ApplyDamage(int damage);
    7. }
     
  6. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    105
    ApplyDamage or TakeDamage? Pick one, and use it consistently. You can't call a method that doesn't exist.
     
    zombiegorilla and bugfinders like this.
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,955
    Closed. Low effort. Please use the learn section and some tutorials rather than posting "how do I fix?"
     
    bugfinders likes this.
Thread Status:
Not open for further replies.