Search Unity

Giving Damage to Other Object | Type of Other Script Not Found | Tried to Fix for 3 Hours Already

Discussion in 'Scripting' started by InvictusCo, Jul 27, 2017.

  1. InvictusCo

    InvictusCo

    Joined:
    Mar 29, 2016
    Posts:
    5
    Hello! I am making a game in Unity using C# and have an enemy that needs to make the player take damage when it gets too close. The enemy will chase the player and I can make the enemy die if it gets too close to the player, but I can't seem to make the player take damage. I've worked on this for over 3 hours before coming here and have tried looking through forums and videos to no avail. Any help is appreciated! Thank you!


    Error I'm Getting:


    Script on Enemy
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Target : MonoBehaviour {
    5.  
    6. public float health = 100f;
    7.  
    8.  
    9.  
    10.     public float deathDistance = 0.5f;
    11.         public Transform thisObject;
    12.         public Transform target;
    13.         private UnityEngine.AI.NavMeshAgent navComponent;
    14.         private Player player;
    15.         public float damage = 10;
    16.    
    17.         public void TakeDamage (float amount)
    18.        
    19.        
    20. {
    21.     health -= amount;
    22.     if (health <= 0f)
    23.     {
    24.         Die();
    25.     }
    26. }
    27.  
    28.     void Die()
    29.     {
    30.         Destroy(gameObject);
    31.     }
    32.    
    33.         void Start()
    34.         {
    35.             target = GameObject.FindGameObjectWithTag("Player").transform;
    36.             navComponent = this.gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
    37.         }
    38.        
    39.         void Update()
    40.     {
    41.            
    42.             float dist = Vector3.Distance(target.position, transform.position);
    43.            
    44.             if(target)
    45.             {
    46.                 navComponent.SetDestination(target.position);
    47.             }
    48.  
    49.             else
    50.             {
    51.                 if(target == null)
    52.                 {
    53.                     target = this.gameObject.GetComponent<Transform>();
    54.                 }
    55.                 else
    56.                 {
    57.                     target = GameObject.FindGameObjectWithTag("Player").transform;
    58.                 }
    59.             }
    60.             if (dist <= deathDistance)
    61.             { //Die();
    62.                
    63.                 if (target != null)
    64.                 {
    65.                     target.GetComponent<PlayerC>().TakeDamage(damage);
    66.                     //Die();
    67.                 }
    68.             }
    69.         }
    70.     }
    71.  
    Script on Player
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class Player : MonoBehaviour {
    6.  
    7. public float health = 100f;
    8.  
    9.         public void TakeDamage (float amount)
    10. {
    11.     health -= amount;
    12.     if (health <= 0f)
    13.     {
    14.         Restart();
    15.     }
    16. }
    17.  
    18.     void Restart()
    19.     {
    20.         SceneManager.LoadScene("test1");
    21.     }
    22.    
    23.  
    24. }
    25.  
     
  2. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Line (65) PlayerC --> Player
     
  3. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    That Should work for you. Now line 65 Player should be blue!
     
  4. InvictusCo

    InvictusCo

    Joined:
    Mar 29, 2016
    Posts:
    5
    Thank you for the help, but now whenever the enemy gets close enough to damage I get this error:

    My player is Target and the script on it is called PlayerC. I now corrected the player script to read
    Code (CSharp):
    1. public class PlayerC : MonoBehaviour {
    at the top, but I'm still getting the error above.
     
    Last edited: Jul 27, 2017
  5. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Target.Update () (at Assets/Scripts/Target.cs:65) is where the new error is occurring, the Target Script, I assumed since you changed the player script you know have to assign it in the inspector. you should have just changed line 65 to read Player instead of PlayerC before changing the entire class name unless that's what you wanted to do.

    But now since you change the class name you may encounter more errors depending what all referenced the Player class before you changed the name.

    But to answer this immediate q: set the new Player(PlayerC) it in the inspector on the target script..
     
  6. InvictusCo

    InvictusCo

    Joined:
    Mar 29, 2016
    Posts:
    5
    Thank you so much for helping me! I found out that it was because I had the player script on the eye (I'm developing for VR) rather than the ears so it was overriding the player tag and setting it back to main camera. All fixed!
     
    TSC likes this.