【问题标题】:Space Invaders With 2 Errors Unity有 2 个错误 Unity 的太空入侵者
【发布时间】:2019-02-02 12:37:31
【问题描述】:

我在增加我的太空入侵者克隆中的分数时遇到了问题。我有 2 个错误。

Assets/Scripts/ScoreManager.cs(26,12):错误 CS0103:名称 `retryLevel' 在当前上下文中不存在

Assets/Scripts/ScoreManager.cs(55,43):错误 CS1061:键入 int' does not contain a definition forTostring' 并且没有扩展方法 可以找到Tostring' of typeint'。你错过了一个程序集 参考?

我应该在我的脚本中的什么地方和什么地方进行更改?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour
{

int i = 0;
public GameObject HighScoreDisplay;
public Text scoreDisplay;
public int score = 0;
public Text[] highScoreTables;


private void Start()
{
  if (HighScoreDisplay == null || scoreDisplay == null)    {

            Debug.LogWarning("Values are missing on the ScoreManager!");

            return;

        }

           retryLevel();

}

private void update()
{
  scoreDisplay.text = score.ToString();

}

public void ModifyScore(int scoreToAdd)
{
  score += scoreToAdd;

}

public void fromScratch(){
  score = 0;
  HighScoreDisplay.SetActive(false);
}

public void PlayerDied()
{


  HighScores.AddScore(score);

  foreach (Text table in highScoreTables)
  {
    table.text = HighScores.scoreTable[i].Tostring();

    i++;
  }

  HighScoreDisplay.SetActive(true);
  score = 0;

}

}

public static class HighScores


{

  public static List<int> scoreTable = new List<int>{0,0,0};

  public static void AddScore(int score)
{
  if (score > scoreTable[2])
  {
    scoreTable[2] = score;
  }
  else if (score > scoreTable[1])
{
  scoreTable[1] = score;
}
else if (score > scoreTable[0])
{
scoreTable[0] = score;
}
}
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    这些错误很简单:

    1. 您的代码不包含函数retryLevel 的定义,我也看不到任何可用于从其他类导入此类函数的静态函数,(它是从MonoBehaviour 继承的函数?你确定它没有根据 C# 标准的大写首字母吗?)

    2. 你有Tostring 和小的S。应该是ToString

    【讨论】:

    • retryLevel 不是从MonoBehaviour 继承的,MonoBehaviour 是一个具有完整文档的 Unity 类。
    • 我已经这么怀疑了。 (不幸的是,我不是 Unity 开发人员,我懒得阅读文档。相反,我完成了自己的项目。) 它仍然可以是一个扩展。但概率很低。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多