【问题标题】:Unity 3D - Too many errorsUnity 3D - 错误太多
【发布时间】:2014-12-06 16:07:25
【问题描述】:

从外观上看,有两个脚本会产生这些错误,它们在下面列出:

------------------ GameInformation - 编码如下: -------------------

using UnityEngine;

使用 System.Collections;

公共类 GameInformation : MonoBehaviour {

void Awake(){
    DontDestroyOnLoad (transform.gameObject);
}

public static string PlayerName{ get; set; }
public static int PlayerLevel{ get; set; }
public static BaseCharacterClass PlayerClass{ get; set; }
public static int Speed{ get; set; }
public static int Endurance{ get; set; }
public static int Strength{ get; set; }
public static int Health{ get; set; }
}

------------------ 另一个脚本是 SaveInformation: -------------------

using UnityEngine;

使用 System.Collections;

公共类保存信息 {

public static void SaveAllInformation(){
    PlayerPrefs.SetInt("PLAYERLEVEL", GameInformation.PlayerLevel);
    PlayerPrefs.SetString("PLAYERNAME", GameInformation.PlayerName);
    PlayerPrefs.SetString("SPEED", GameInformation.Speed);
    PlayerPrefs.SetString("ENDURANCE", GameInformation.Endurance);
    PlayerPrefs.SetString("STRENGTH", GameInformation.Strength);
    PlayerPrefs.SetString("HEALTH", GameInformation.Health);
    }

}

------------------ 错误: -------------------

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(7,67):错误 CS0117:GameInformation' does not contain a definition for PlayerLevel'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(7,29):错误 CS1502:最好的重载方法匹配 `UnityEngine.PlayerPrefs.SetInt(string, int)' 有一些无效 论据

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(7,29):错误 CS1503:参数 #2' cannot convertobject' 表达式类型为 `int'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(8,69):错误 CS0117:GameInformation' does not contain a definition for PlayerName'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(8,29):错误 CS1503:要键入的参数 #2' cannot convertobject' 表达式 `字符串'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(9,64):错误 CS0117:GameInformation' does not contain a definition forSpeed'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(9,29):错误 CS1502:最好的重载方法匹配 `UnityEngine.PlayerPrefs.SetString(string, string)' 有一些无效 论据

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(9,29):错误 CS1503:要键入的参数 #2' cannot convertobject' 表达式 `字符串'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(10,68):错误 CS0117:GameInformation' does not contain a definition for Endurance'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(10,29):错误 CS1502:最好的重载方法匹配 `UnityEngine.PlayerPrefs.SetString(string, string)' 有一些无效 论据

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(10,29):错误 CS1503:要键入的参数 #2' cannot convertobject' 表达式 `字符串'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(11,67):错误 CS0117:GameInformation' does not contain a definition forStrength'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(11,29):错误 CS1502:最好的重载方法匹配 `UnityEngine.PlayerPrefs.SetString(string, string)' 有一些无效 论据

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(11,29):错误 CS1503:要键入的参数 #2' cannot convertobject' 表达式 `字符串'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(12,65):错误 CS0117:GameInformation' does not contain a definition forHealth'

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(12,29):错误 CS1502:最好的重载方法匹配 `UnityEngine.PlayerPrefs.SetString(string, string)' 有一些无效 论据

资产/标准 资产/脚本/SavingAndLoading/SaveInformation.cs(12,29):错误 CS1503:要键入的参数 #2' cannot convertobject' 表达式 `字符串'

-------------------

在回复我时请记住,我对编码还比较陌生。谢谢!

-------------------

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    在编程世界中,这些都是非常基本的错误,如果您了解它们,而不是仅仅下载脚本并希望它们全部连接在一起,您会发现进步会容易得多。下面我描述了每个错误的原因,让您开始:

    GameInformation' does not contain a definition for
      PlayerLevel'
    // This one means you're talking about PlayerLevel 
    // in GameInformation, but GameInformation doesn't have PlayerLevel
    
    The best overloaded method match for `UnityEngine.PlayerPrefs.SetInt(string, int)' has some invalid arguments
    // This means that you're trying to call SetInt with something that isn't a string
    // or something that isn't an int
    
    Argument #2' cannot convert object' expression to type `int'
    // Same as above, trying to give *object* to something that expects *int*
    
    GameInformation' does not contain a definition for
      PlayerName'
    // GameInformation doesn't have PlayerName either
    
    Argument #2' cannot convertobject' expression to type `string'
    // Can't put an *object* Type into *string*
    
    GameInformation' does not contain a definition forSpeed'
    // You can probably guess that this means that there is no Speed in GameInformation
    
    GameInformation' does not contain a definition for
     Endurance'
    // You guessed it, no Endurance in GameInformation :)
    
    The best overloaded method match for `UnityEngine.PlayerPrefs.SetString(string, string)' has some invalid arguments
    // Based on the other errors, you're probably trying to pass *object* as
    // a *string*
    

    基本上,这些错误都归结为一件事:您的 GameInformation 类没有您引用的属性(耐力、速度等),这让编译器感到不安。

    【讨论】:

    • 从我的回答中可以推断,要么添加属性,要么删除对不存在的属性的引用。我回复的第二个含义是,如果你无法修复它,你可以学习。这是一组教程,您可以实际学习如何编程:tutorialspoint.com/csharp/index.htm
    • 是的,但是添加属性是什么意思!?
    • 我该如何添加它们!?
    猜你喜欢
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多