unity 成员修饰符 public 必须在前面

unity member modifier public must precede

我已经解决了这个问题,但是当事情变得更加疯狂时,我意识到尝试并完成这个是多么愚蠢,因为它显然最终没有任何好处。

如果您有类似的问题,请随时查看本文,也许您会学到一两件事。 Idk¯\_(ツ)_/¯

有人在我之前在这个网站上问过这个问题,但其中 none 确实是同一个问题。 我敢肯定,我可能错过了什么检查了我的代码,但我不知道它是什么。现在 Unity 烦扰我说我没有把 public 修饰符放在正确的位置,即使它正是我所看到的应该在的位置。

让我快速告诉你...


Unity returns 给我的错误:

Assets\saveFiles.cs(15,1): error CS1585: Member modifier 'public' must precede the member type and name

我的代码(我对post这么长的代码有点疯狂,只是忽略那些我显然懒得删除的评论。那是为了当我进一步开发时。)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Security.Permissions; // Allows permission requests from within         script. VERY IMPORTANT.
using System.Security.AccessControl; // Also as important for the same reason.

[ComVisibleAttribute(true)]
public sealed class FileIOPermission{}(AllAccess View, string MyDocuments) // Grants access to the User's Documents folder.

public class saveFiles : MonoBehaviour {
    [DllImport("System.Windows.Forms.dll")]
    private static extern void SystemWindows();
    [DllImport("System.Drawing.dll")]
    private static extern void SystemDrawing();

    public string MyDocuments = System.Environment.GetFolderPath( (System.Environment.SpecialFolder.MyDocuments) );

    // Reserve these variables for save file memory.
        public string UserData;
        public string UserConfig;
        public bool IsUserAvailable;

    void Start () {
        // On game execution, check for the game's configuration files. Load if they exist, save blank ones if they don't.
        // (Don't save in the Steam client's user data directory until you are aware of a way to find which user is logged into Steam.)

    // Check for saved progress and load it into memory if available. If not, it creates one and prepares it for editing.
        if(System.IO.File.Exists(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.MyDocuments) ) + "/SaveData/tanky bois/" + "user.sav") == true) {
            UserData = System.IO.File.ReadAllText(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.MyDocuments) ) + "/SaveData/tanky bois/" + "user.sav");
        } else {
            System.IO.File.Create(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.MyDocuments) ) + "/SaveData/tanky bois/" + "user.sav");
            UserData = "statsA{\"xp\" : 0, \"money\" : 2000, \"MatchInProgress\" : false} Inventory{\"Default\":1;} loadout{1 null null null null}";
            System.IO.File.WriteAllText(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.MyDocuments) ) + "/SaveData/tanky bois/" + "user.sav", UserData);
            // Rules for UserData string:
            //      Inventory is stored as: "[ItemTag string]:[QuantityOfItem integer];"
            //
            //    Optionally, you can add extra info about the item from within a parentheses, placed after [ItemTag]. Useful for upgrades and equipment, leveling up, etc.
            //     (e.g. "Default(  [JSON METADATA]  ):1;")
            //
            //      Anything in quotes is a string, otherwise it is an integer or a boolean value depending on the returned value.
            //  (e.g. 24 as an integer, true/false/null as a boolean)
            //
            //      Loadout section is stored as 5 different inventory slot numbers, seperated by spaces.
            //  Each number represents which item listing, counting from the start to the end of the Inventory string.
            //  (e.g. "Inventory{weaponA:1;weaponB:1;weaponC:1;weaponD:1;} loadout{1 3 4 null null}" 
            //  will return WeaponA in slot 1, weaponC in slot 2, weaponD in slot 3, and slots 4 and 5 empty)
            //
            //      Majority of user save data will use JSON format or one of a similar syntax.
            //      (This applies to pretty much 100% of the settings file.)
        }

    // Check for user settings and load it into memory if available. If not, it creates one and prepares it for editing.
        if(System.IO.File.Exists(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.MyDocuments) ) + "/SaveData/tanky bois/" + "cfg") == true){
            UserConfig = System.IO.File.ReadAllText(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.MyDocuments) ) + "/SaveData/tanky bois/" + "cfg");
        } else {
            System.IO.File.Create(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.MyDocuments) ) + "/SaveData/tanky bois/" + "cfg");
        };
    }
    public void saveUsr() {
        // Code for Windows Standalone:
        System.IO.File.WriteAllText(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.MyDocuments) ) + "/SaveData/tanky bois/" + "user.sav", UserData);
        System.IO.File.WriteAllText(System.Environment.GetFolderPath( (System.Environment.SpecialFolder.MyDocuments) ) + "/SaveData/tanky bois/" + "cfg", UserConfig);
    }

}

所以在这种情况下,为什么 Unity 开始发疯要求我添加一个 "public" 修饰符?

如果我遗漏了什么,请告诉我。

谢谢。

让我们重新格式化您的代码以使其更易于阅读:

....
[ComVisibleAttribute(true)]
public sealed class FileIOPermission
{
}

(AllAccess View, string MyDocuments) // Grants access to the User's Documents folder.

public class saveFiles : MonoBehaviour {
....

你现在能看出问题所在了吗?

(AllAccess View, string MyDocuments) 不在任何 class 中,因此编译器试图弄清楚这意味着什么,并且它认为您正在尝试声明 class 或接口或一个枚举,最好的猜测是你缺少一个修饰符。

在我看来,您似乎是从其他地方错误地复制并粘贴了这一行。你应该删除它。