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

Scripting help! Please!

Discussion in 'Scripting' started by Ralph1388, Aug 19, 2015.

  1. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    So i am starting working on some scripting for health and damage for me and the enemy but there are some small things i am uncertain about..

    This is the Damage script for my player..

    Code (CSharp):
    1.  
    2. public class PlayerController : MonoBehaviour
    3. public GameObject shot;
    4.     public Transform shotSpawn;
    5.     public float fireRate;
    6.     public int damagePerShot = 20;
    7.  
    8.     private float nextFire;
    9.  
    10.     void Update ()
    11.     {
    12.         if (Input.GetButton("Fire1") && Time.time > nextFire)
    13.         {
    14.             nextFire = Time.time + fireRate;
    15.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    16.             GetComponent<AudioSource>().Play ();
    17.  
    18.         }
    19.     }
    20.  
    21.     void OnCollisionEnter(Collision shot)
    22.         {
    23.             if(shot.collider.tag == "Enemy")
    24.                 {        
    25.                     EnemyHealth -= 20; //Bullet strength is 20
    26.                 }    
    27.          
    28.         }
    and this is the health script for the enemy
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EnemyHealth : MonoBehaviour
    4. {
    5.     public int startingHealth = 100;
    6.     public int currentHealth;
    7.  
    8.  
    9.  
    10.  
    11.     void Awake ()
    12.     {
    13.         currentHealth = startingHealth;
    14.     }
    15.  
    16.    
    17.     public void TakeDamage (int amount, Vector3 hitPoint)
    18.     {
    19.      
    20.         currentHealth -= amount;
    21.          
    22.         if(currentHealth <= 0)
    23.         {
    24.             Destroy (this.gameObject);
    25.         }
    26.     }
    27.            
    28. }
    29.  
    For now its giving me this error i know maybe later it will give me more:

    error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected

    and maybe if i made a mistake you guys can help me out also i am struggling with changing the health script to collaborate with the damage script
     
    Last edited: Aug 19, 2015
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    It's giving you that error on which line of which file?
     
  3. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    Can you be more elaborate
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    No, the question is whether you can elaborate. The error message in Unity tells you not just the text of the error, but also what file, and which line of that file, it occurs on.

    But you haven't told us that. You've given us only the text of the error message, not which file or line number it is on. So you're asking us to read through all your code and find where it might be.
     
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    When an error comes up, it tells you the file name and line with the error (usually looks like this FileName:LineNumber).

    Anyways, the error is here:
    Code (csharp):
    1.  
    2. EnemyHealth -= 20; //Bullet strength is 20
    3.  
    You're trying to mathematically manipulate the name of a class, which doesn't make a whole lot of sense. You probably want to do something more like this:

    Code (csharp):
    1.  
    2. if(shot.collider.tag == "Enemy")
    3. {
    4.     EnemyHealth enemyHealth = shot.gameObject.GetComponent<EnemyHealth>();
    5.  
    6.     if(enemeyHealth != null)
    7.         enemeyHealth.TakeDamage(20);
    8. }
    9.  
    So first we use GetComponent<EnemyHealth>() to get the EnemyHealth component from the colliding GameObject, then check if it's valid (to make sure the GameObject actually has an EnemyHealth component) and call the TakeDamage method appropriately.
     
    DonLoquacious and JoeStrout like this.
  6. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    Well it gives me this error...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.  
    10. public class PlayerController : MonoBehaviour
    11. {
    12.     public float speed;
    13.     public float tilt;
    14.     public Boundary boundary;
    15.  
    16.     public GameObject shot;
    17.     public Transform shotSpawn;
    18.     public float fireRate;
    19.     public int damagePerShot = 20;
    20.  
    21.     private float nextFire;
    22.    
    23.     void Update ()
    24.     {
    25.         if (Input.GetButton("Fire1") && Time.time > nextFire)
    26.         {
    27.             nextFire = Time.time + fireRate;
    28.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    29.             GetComponent<AudioSource>().Play ();
    30.  
    31.         }
    32.     }
    33.    
    34.     void OnCollisionEnter(Collision shot)
    35.         {  
    36.             if(shot.collider.tag == "Enemy")
    37.                 {
    38.                     EnemyHealth enemyHealth = shot.gameObject.GetComponent<EnemyHealth>();
    39.            
    40.                     if(enemyHealth != null)
    41.                     enemyHealth.TakeDamage(20);
    42.                 }      
    43.            
    44.         }
    Assets/GAME/Scripts/Player/PlayerController.cs(41,53): error CS1501: No overload for method `TakeDamage' takes `1' arguments
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,150
    You have a method called TakeDamage in the health script. It requires two arguments. You are trying to feed it only one.
     
  8. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    So where would i fix it? within the health script or damage?
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,150
    Where you change it depends on your original intentions. Did you intend for TakeDamage to require a Vector3? If not, remove it from the definition and it'll work. If you did, you need to supply the appropriate Vector3 to the method call in the damage script.
     
  10. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    So if i intended to put a vector3 what do i put in the brackets? sorry for the spoom feeding but ive been trying to make it work for 4 days now
     
  11. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Changing line 17 to
    Code (csharp):
    1. publicvoid TakeDamage (int amount)
    would work as you are not using the Vector3 anyway, unless you are planning on adding more code that requires it,
     
  12. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    But thats when you dont have vector3 but i am using vector 3
     
  13. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    What was your intention when you wrote this line?
    Code (CSharp):
    1. public void TakeDamage (int amount, Vector3 hitPoint)
     
  14. Ralph1388

    Ralph1388

    Joined:
    Jul 25, 2015
    Posts:
    24
    to take of the amount that the bolt does as damage and subtrct it as health of the enemy
     
  15. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Then you should remove the , Vector3 hitPoint part from it.
     
    JoeStrout likes this.