简体   繁体   English

适用于Unity3d的C#:3个无效令牌,名称空间不能直接包含…,并且文件结尾应为预期

[英]C# for Unity3d: 3x invalid tokens, name space cannot directly contain…, and end of file expected

so I'm trying to make a basic death script that changes the view from the main character controller to a second camera. 因此,我试图制作一个基本的死亡脚本,以将视图从主角色控制器更改为第二台摄像机。

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

if I remove the struct at line 2, all the errors go away except the one on line 4 (if (isDead)) and it gets "a namespace does not directly contain members such as fields or methods" I have a bit of experience with programming in python, C++, and Java, but I really just started c# yesterday, so any help would be greatly appreciated. 如果我在第2行删除了该结构,那么除第4行(如果是(isDead))上的错误之外,所有其他错误都会消失,并且它得到“命名空间不直接包含诸如字段或方法之类的成员”的经验,我对此有一点经验使用python,C ++和Java进行编程,但我昨天才真正开始使用c#,因此,我们将不胜感激。 Thanks in advance 提前致谢

EDIT: 编辑:

I sort of fixed it, but now I get "the name 'xxx' does not exist in the current context", for isdead, deathcamtag, and fpscontrollertag, unless I change public class DeathCamSwitch to HealthScript, but then I get "the namespace '' already contrains a definition for 'healthscript'" so it doesn't work if I don't link the two classes but when I do, there's 2 definitions. 我对其进行了某种程度的修复,但是对于isdead,deathcamtag和fpscontrollertag,现在得到的名称为“'xxx'在当前上下文中不存在”,除非将公共类DeathCamSwitch更改为HealthScript,但随后得到的是“名称空间” '已经约束了'healthscript'的定义”,因此如果我不链接这两个类,则它不起作用,但是当我这样做时,有2个定义。

You have to move your code to some method declaration. 您必须将代码移至某些方法声明。 Also I would say that you have to use class instead of struct and inherit from MonoBehaviour 我还要说的是,您必须使用类而不是struct并从MonoBehaviour

Here is an example of a simple scene changer class: 这是一个简单的场景更改器类的示例:

public class SceneChanger : MonoBehaviour {

    public string SceneName;

    void OnMouseDown(){

        Application.LoadLevel(SceneName);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM