-1
using UnityEngine;
using System.Collections;

public class Sword : MonoBehaviour {

var totalhealth = 100;

    function OnTriggerEnter(other : Collider){
        if(other.tag == "angelic_sword_02"){
            totalhealth -= 50;
        }
    }

    function Update(){
        if(totalhealth <= 0){
            Destroy(gameObject);
        }
    }
}

I get an "Identifier Expected" in the script where it says in the line

function OnTriggerEnter(other : Collider) {

Any help please?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

2

You are using incorrect syntax for a C# method. Unity supports multiple languages for user code. Perhaps you copied an example from a different language?

function OnTriggerEnter(other : Collider){
    if(other.tag == "angelic_sword_02"){
        totalhealth -= 50;
    }
}

Should be closer to

public void OnTriggerEnter(Collider other){
    if(other.tag == "angelic_sword_02"){
        totalhealth -= 50;
    }
}
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Thank you! That helped me out, and I guess I'm not that good at scripting yet, and I'm still studying both C# and Javascript, so I tend to mix both of them up. But now I'm getting a new error for "var". I forgot what you put there instead. I know it's not "function" because that's Javascript.... right? You put "void' instead...right? – thebandgeekgamer May 08 '15 at 01:35
  • 1
    We all start somewhere. It does look a lot like JavaScript. Just change `var` to `int`. – Eric J. May 08 '15 at 01:37
0

I finally figured it out. And I have no more compiler errors anymore.

using UnityEngine;
using System.Collections;

public class Sword : MonoBehaviour {

public float totalhealth = 100;

public void OnTriggerEnter(Collider other){
    if(other.tag == "angelic_sword_02"){
        totalhealth -= 50;
    }
}

void Update(){
    if(totalhealth <= 0){
        Destroy(gameObject);
    }
}
}

@EricJ thank you for helping me out. :)