"标识符预期"错误?

时间:2015-05-08 01:14:38

标签: c# unity3d compiler-errors unityscript identifier

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

我得到一个&#34;标识符预期&#34;在脚本中所说的

function OnTriggerEnter(other : Collider) {

请帮忙吗?

2 个答案:

答案 0 :(得分:2)

您对C#方法使用的语法不正确。 Unity支持用户代码的多种语言。也许您从其他语言复制了一个示例?

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

应该更接近

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

答案 1 :(得分:0)

我终于明白了。而且我不再有编译错误了。

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谢谢你的帮助。 :)