Unity3D 错误 CS0229 - 变量之间的错误歧义
Unity3D Error CS0229 - false ambiguity between variables
我在使用 Unity(版本 2020.3.5f1 LTS)时遇到问题。特别是我把一个项目从一台电脑移到另一台电脑上,多次出现这个错误:
Error CS0229 Ambiguity between 'TimeCounter.singleton' and 'TimeCounter.singleton'
我知道当你有两个同名的不同变量时会发生这种错误,因为编译器无法区分它们;但是,我没有这种特殊情况。其实我的项目里只有一个classTimeCounter
,没有重复的变量名
我曾尝试通过删除 .sln 项目并通过 Unity 重新创建它来解决此问题(按照建议 ),但是没有成功。
产生错误的class如下:
using System.Collections;
using UnityEngine;
using EventSystem2;
public class TimeCounter : MonoBehaviour
{
[SerializeField] float minutesToPlay = 2f;
float currentTime = 0f;
public GameEvent endedTimeEvent;
private static TimeCounter singleton;
void Awake()
{
if (!singleton)
{
singleton = this;
DontDestroyOnLoad(this);
}
}
void Start()
{
currentTime = minutesToPlay * 60f;
}
IEnumerator Timer()
{
while (true)
{
if (currentTime > 0f)
{
currentTime -= 1f;
print("current time: " + currentTime);
}
else
{
endedTimeEvent.Raise();
}
yield return new WaitForSeconds(1);
}
}
public void StartTimer()
{
StartCoroutine(nameof(Timer));
}
public void StopTimer()
{
StopCoroutine(nameof(Timer));
}
public void ResetTimer()
{
currentTime = minutesToPlay * 60f;
}
public float GetTimeToPlayInMilliseconds() => (this.minutesToPlay * 60000);
public float GetTimeToPlayInSeconds() => (this.minutesToPlay * 60);
}
这是我遇到的歧义错误列表:
问题解决了! TimeCounter.cs
脚本在Unity项目中重复了(我不知道为什么会这样),因此编译不知道我指的是哪个版本。
我在使用 Unity(版本 2020.3.5f1 LTS)时遇到问题。特别是我把一个项目从一台电脑移到另一台电脑上,多次出现这个错误:
Error CS0229 Ambiguity between 'TimeCounter.singleton' and 'TimeCounter.singleton'
我知道当你有两个同名的不同变量时会发生这种错误,因为编译器无法区分它们;但是,我没有这种特殊情况。其实我的项目里只有一个classTimeCounter
,没有重复的变量名
我曾尝试通过删除 .sln 项目并通过 Unity 重新创建它来解决此问题(按照建议
产生错误的class如下:
using System.Collections;
using UnityEngine;
using EventSystem2;
public class TimeCounter : MonoBehaviour
{
[SerializeField] float minutesToPlay = 2f;
float currentTime = 0f;
public GameEvent endedTimeEvent;
private static TimeCounter singleton;
void Awake()
{
if (!singleton)
{
singleton = this;
DontDestroyOnLoad(this);
}
}
void Start()
{
currentTime = minutesToPlay * 60f;
}
IEnumerator Timer()
{
while (true)
{
if (currentTime > 0f)
{
currentTime -= 1f;
print("current time: " + currentTime);
}
else
{
endedTimeEvent.Raise();
}
yield return new WaitForSeconds(1);
}
}
public void StartTimer()
{
StartCoroutine(nameof(Timer));
}
public void StopTimer()
{
StopCoroutine(nameof(Timer));
}
public void ResetTimer()
{
currentTime = minutesToPlay * 60f;
}
public float GetTimeToPlayInMilliseconds() => (this.minutesToPlay * 60000);
public float GetTimeToPlayInSeconds() => (this.minutesToPlay * 60);
}
这是我遇到的歧义错误列表:
问题解决了! TimeCounter.cs
脚本在Unity项目中重复了(我不知道为什么会这样),因此编译不知道我指的是哪个版本。