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 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
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;
}
}
}
}
It worked, thank you 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?
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.