ali*_*rek 3 c# unity-game-engine
我在使用 Unity(版本 2020.3.5f1 LTS)时遇到问题。特别是,我将一个项目从一台计算机移至另一台计算机,并且多次出现此错误:
错误 CS0229 “TimeCounter.singleton”和“TimeCounter.singleton”之间存在歧义
我知道当你有两个同名的不同变量时会发生这种错误,因为编译器无法区分它们;不过,我没有这种特殊情况。事实上,TimeCounter
我的项目中只有一个类,并且没有重复的变量名称。
我尝试通过删除 .sln 项目并通过 Unity 重新创建它(如此处的建议)来解决此问题,但它不起作用。
产生错误的类如下:
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);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1070 次 |
最近记录: |