首页 > 解决方案 > unity 成员修饰符 public 必须在前面

问题描述

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

如果您遇到类似问题,请随时查看此内容,也许您会学到一两件事。Idk¯\_(ツ)_/¯

人们在我之前在这个网站上问过这个问题,但他们都不是真正的同一个问题。我确信当我检查我的代码时我可能错过了一些东西,但我无法弄清楚它是什么。现在 Unity 困扰我,据说我没有将public修饰符放在正确的位置,即使它正是它应该在的位置,从我所看到的。

让我快速告诉你...


错误 Unity 返回给我:

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

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

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 怎么会开始发疯,要求我在已经有的地方添加一个“公共”修饰符?

如果我错过了什么,请告诉我。

谢谢你。

标签: c#debuggingunity3dc#-4.0

解决方案


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

....
[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)不在任何类中,因此编译器试图弄清楚这意味着什么,它认为您正在尝试声明一个类或接口或枚举,最好的猜测是您缺少修饰符。

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


推荐阅读