Search code examples
c#unity-game-enginecompiler-errorsunityscriptidentifier

"Identifier Expected" error?


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?


Solution

  • 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;
        }
    }