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

IDamageable could not be found?

Discussion in 'Scripting' started by CoopReed, Jan 5, 2016.

  1. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public interface IDamageable
    4. {
    5.  
    6.     void TakeHit(float damage, RaycastHit hit);
    7.  
    8. }
    Hey guys, following tutorial and I needed to use IDamageable. Came up as not being found

    Class 1
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Projectile : MonoBehaviour
    5. {
    6.  
    7.     public LayerMask collisionMask;
    8.     float speed = 10;
    9.     float damage = 1;
    10.  
    11.     public void SetSpeed(float newSpeed)
    12.     {
    13.         speed = newSpeed;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         float moveDistance = speed * Time.deltaTime;
    19.         CheckCollisions(moveDistance);
    20.         transform.Translate(Vector3.forward * moveDistance);
    21.     }
    22.  
    23.  
    24.     void CheckCollisions(float moveDistance)
    25.     {
    26.         Ray ray = new Ray(transform.position, transform.forward);
    27.         RaycastHit hit;
    28.  
    29.         if (Physics.Raycast(ray, out hit, moveDistance, collisionMask, QueryTriggerInteraction.Collide))
    30.         {
    31.             OnHitObject(hit);
    32.         }
    33.     }
    34.  
    35.     void OnHitObject(RaycastHit hit)
    36.     {
    37.         IDamageable damageableObject = hit.collider.GetComponent<IDamageable>();
    38.         if (damageableObject != null)
    39.         {
    40.             damageableObject.TakeHit(damage, hit);
    41.         }
    42.         GameObject.Destroy(gameObject);
    43.     }
    44. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ok, silly question to start: is the interface script saved in the project somewhere?
     
  3. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    An even sillyer anwser, how would I know if it is saved ?
     
  4. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    When you noted not being found, is it a compile or runtime error?

    If compile, check that your IDamageable is indeed saved in your project as @LeftyRighty suggested. If still not found, closing and reopening MonoDevelop sometimes helped me with the issue.

    If you can't find IDamageable with GetComponent<IDamageable>(), make sure you are using Unity 5+ since previous versions do not fetch interfaces with that function because of the "where T : Component". In previous versions you had to get interfaces using the snippet provided in https://www.reddit.com/r/Unity3D/comments/34spf6/implementing_getinterface_extension_on_objects/

    Or alternatively you can use typeof(T) approach:
    Code (CSharp):
    1. IDamageable dmg = gameObject.GetComponent(typeof(IDamageable));
    Hope this helps!
     
    CoopReed likes this.
  5. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    Hey mate, I want to thank you very much for taking time out of your day to help me. You have helped me out so much, thanks for putting so much detail and describing how to fix the problem.

    Thanks so much!