'Unity3D Error CS0229 - false ambiguity between variables
I have a problem with Unity (version 2020.3.5f1 LTS). In particular, I have moved a project from a computer to another, and I have this error several times:
Error CS0229 Ambiguity between 'TimeCounter.singleton' and 'TimeCounter.singleton'
I know that this kind of error occurs when you have two different variables with the same name, as the compiler cannot distinguish between them; however, I do not have this particular situation. In fact, I have only one class TimeCounter in my project, and there are no repeated variables' names.
I have tried to fix this problem by deleting the .sln project and recreating it through Unity (as suggested here), however it did not work.
The class in which the error generates is the following one:
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);
}
Solution 1:[1]
Problem solved! The TimeCounter.cs script was duplicated inside the Unity project (I do not know why it happened), therefore the compiled did not know to which version I was referring to.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | alirek |

