Scripting help! Please!

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…

public class PlayerController : MonoBehaviour
public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;
    public int damagePerShot = 20;

    private float nextFire;
 
    void Update ()
    {
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
            GetComponent<AudioSource>().Play ();

        }
    }
 
    void OnCollisionEnter(Collision shot)
        { 
            if(shot.collider.tag == "Enemy")
                {         
                    EnemyHealth -= 20; //Bullet strength is 20
                }     
         
        }

and this is the health script for the enemy

using UnityEngine;

public class EnemyHealth : MonoBehaviour
{
    public int startingHealth = 100;
    public int currentHealth;
 

 

    void Awake ()
    {
        currentHealth = startingHealth;
    }

    
    public void TakeDamage (int amount, Vector3 hitPoint)
    {
     
        currentHealth -= amount;
         
        if(currentHealth <= 0)
        {
            Destroy (this.gameObject);
        }
    }
           
}

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

It’s giving you that error on which line of which file?

Can you be more elaborate

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.

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:

EnemyHealth -= 20; //Bullet strength is 20

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:

if(shot.collider.tag == "Enemy")
{
    EnemyHealth enemyHealth = shot.gameObject.GetComponent<EnemyHealth>();

    if(enemeyHealth != null)
        enemeyHealth.TakeDamage(20);
}

So first we use GetComponent() 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.

2 Likes

Well it gives me this error…

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour
{
    public float speed;
    public float tilt;
    public Boundary boundary;

    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;
    public int damagePerShot = 20;

    private float nextFire;
   
    void Update ()
    {
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
            GetComponent<AudioSource>().Play ();

        }
    }
   
    void OnCollisionEnter(Collision shot)
        {   
            if(shot.collider.tag == "Enemy")
                {
                    EnemyHealth enemyHealth = shot.gameObject.GetComponent<EnemyHealth>();
           
                    if(enemyHealth != null)
                    enemyHealth.TakeDamage(20);
                }       
           
        }

Assets/GAME/Scripts/Player/PlayerController.cs(41,53): error CS1501: No overload for method TakeDamage' takes 1’ arguments

You have a method called TakeDamage in the health script. It requires two arguments. You are trying to feed it only one.

So where would i fix it? within the health script or damage?

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.

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

Changing line 17 to 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,

But thats when you dont have vector3 but i am using vector 3

What was your intention when you wrote this line?

public void TakeDamage (int amount, Vector3 hitPoint)

to take of the amount that the bolt does as damage and subtrct it as health of the enemy

Then you should remove the , Vector3 hitPoint part from it.

1 Like