C#对于Unity3d:3x无效令牌,名称空间不能直接包含…,并且应为文件结尾

C#对于Unity3d:3x无效令牌,名称空间不能直接包含…,并且应为文件结尾,c#,unity3d,C#,Unity3d,因此,我试图制作一个基本的死亡脚本,将主角色控制器的视图更改为第二个摄影机 using UnityEngine; struct Death { if (isDead) //invalid token "if" and ")" { DeathCamTag.SetActive(true); //invalid token "(" } else // a namespace does not directly contain members such as fields or methods

因此,我试图制作一个基本的死亡脚本,将主角色控制器的视图更改为第二个摄影机

using UnityEngine;
    struct Death
{
if (isDead) //invalid token "if" and ")"
{
DeathCamTag.SetActive(true); //invalid token "("
}
else // a namespace does not directly contain members such as fields or methods
{
FPSControllerTag.SetActive(true);   
} 
} //type or namespace definition, or end-of-file expected
如果我删除第2行的结构,除了第4行的错误(if(isDead))之外,所有的错误都会消失,它会得到“名称空间不直接包含字段或方法之类的成员” 我在Python、C++和java方面有一些编程经验,但我昨天才真正开始C语言,所以任何帮助都会非常感谢。提前谢谢

编辑:


我已经修复了它,但是现在我得到了isdead、deathcamtag和fpscontrollertag的“名称'xxx'在当前上下文中不存在”,除非我将公共类DeathCamSwitch更改为HealthScript,但是我得到了“名称空间''已经与'HealthScript'的定义相矛盾”,因此如果我不链接这两个类,那么它就不起作用了,但是当我链接时,有两种定义。

您必须将代码移动到某个方法声明中。我还要说,您必须使用class而不是struct,并从
monobhavior

下面是一个简单场景变换器类的示例:

public class SceneChanger : MonoBehaviour {

    public string SceneName;

    void OnMouseDown(){

        Application.LoadLevel(SceneName);

    }
}

您必须将代码移动到某个方法声明中。我还要说,您必须使用class而不是struct,并从
monobhavior

下面是一个简单场景变换器类的示例:

public class SceneChanger : MonoBehaviour {

    public string SceneName;

    void OnMouseDown(){

        Application.LoadLevel(SceneName);

    }
}