【问题标题】:I cant fix error CS1001: Identifier expected我无法修复错误 CS1001:需要标识符
【发布时间】:2021-07-13 15:05:12
【问题描述】:

这是我的移动平台播放器附加代码:

我收到错误 CS1001: Identifier expected for some reason ant youtube 没有帮助我...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAttach : MonoBehaviour

{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        function OnTriggerEnter(other.Collider){

            if (other.gameObject.tag == "player")
            {
                transform.parent = other.transform;

            }
        }


        function OnTriggerExit(other.Collider){
            if (other.gameObject.tag == "player")
            {
                transform.parent = null;

            }
        }
    }
}

【问题讨论】:

  • 欢迎来到 SO。请尽可能详细。你在哪一行得到错误?确切的错误信息是什么?另请阅读How to Ask
  • function OnTriggerEnter(other.Collider) - 那是 not C# 语法。看起来更像是您从 javascript 世界中复制粘贴了一些东西?另外:欢迎来到stackoverflow。我推荐taking the tour,以及阅读how to ask a good questionwhat's on topic
  • 我收到 CS1001: Identifier expected 错误,我在 youtube 上观看了 Unity 教程:youtu.be/9KdY4mafG_E?t=347

标签: c# unity3d


【解决方案1】:

您拥有的是unityscript,它很长已弃用,并且与c# 不兼容!

通常将 MonoBehaviour 消息嵌套在 Update 下!否则消息传递系统将找不到它们,并且根本不会调用它们。

你的班级应该是这样的

public class PlayerAttach : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        // Rather use CompareTag instead of ==
        // the latter fails silently in case of s typo making your debugging live only harder
        if (!other.CompareTag("player")) return;
        
        transform.parent = other.transform;
    }

    private void OnTriggerExit(Collider other)
    {
        if (!other.CompareTag("player")) return;
        
        transform.parent = null;
    }
}

还有一个合乎逻辑的问题:如果你将玩家对象设为父对象,那么玩家对象离开这个碰撞器的具体情况如何,所以每当玩家对象移动时,这个对象就会随之移动?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多