Minor error on a enemy health script.

Hello Unity community,
I’m brand new to code in c#. I have only been learning it for about a week so every bit of constructive criticism is welcome or tips and tricks to help learn is appreciated :slight_smile: I’m having an error on line 19 and it says “A namespace does not directly contain members such as fields or methods.” On the 28th line it says “Type namespace definition, or end of file expected”. This is probably a minor error but I’ve been looking for a way round it for about an hour now. Thank you :slight_smile:

using UnityEngine;
using System.Collections;

public class CombatScript : MonoBehaviour
{  
    public int currentHealth = 6;
    public int enemyHealth = 6;
    public void OnTriggerEnter(Collider other)
    {
        if (other.transform.tag == "Hammer")
        {
            if(enemyHealth <= 0)
            {
              Destroy (gameObject);
            }
        }
    }
}
{
    void OncollisionEnter (Collision collision)
    {
        if(Collision.gameObject.tag == "Hammer")
        {
            currentHealth -= 2;
            print ("Hit")
        }
    }
}

{
    void adjustHealth()
    {
        if(currentHealth < 0)
        {
            currentHealth = 0;

        if(currentHealth > enemyHealth)
            {

            currentHealth = enemyHealth;
            }
        }
    }
}

brackets fail :wink:
remove the brackets at line 18 and 19 and at 28 and 30

your class is closed at the end. your methods belong to the class and are therefore within its brackets

It worked, thank you :slight_smile: but now I’m getting a error on my 21st line says “An object is required for the non-static field, method, or property ‘UnityEngine.Collision.gameObject.get’” I thought the “hammer” was the object reference? its a child so do i need to a child version of gameObject?

Lower case c on Collision for that line.

Quick addition, the oncollisionenter function needs a capital C as the function is case sensitive and wont get called otherwise.

yes because
if(Collision.gameObject.tag == “Hammer”)

the Collision is the class, not your object.
You would need to use “collision” with the minor c as this is your object from the parameter

Just 2 more words on the “what is an object” part

class A
{
  public static int x;
}

class B
{
  void foo()
  {
    A thisIsAnObject = new A();
    A.x = 5;
  }
}

So i can create an Object of A (in this example in class B) - so far so good.
But i could also access “x” anywhere i want without creating an object first as it is static. Which basically means it belongs to the class and not to the object you create.
This being said this also means there is just one “x”.
so by writing Collision.gameObject.tag == “whatever” you assumed there is a static field “tag” in the class Collision.gameobject which you want to access. This is obviously not the case. if it would - the tag would be the same for all GameObjects and not only for the one you created an Instance of.