The namespace is not the issue here. Namespaces are used to organize code in logical groups and have no influence on the actual functioning of the code.
If you create a namespace named “CD”, you can indeed use it in other namespaces by referencing it via “using CD”. You should be able to create the CountDown class just fine then.
I’ve written the following without actually being behind Unity, so there might be a spelling error (notepad FTW!):
// CountDown class in CD namespace
namespace CD
{
public class CountDown: MonoBehaviour
{
public bool countEnd = false;
private void StartCD(string messaggio, int maxSec, int minSec)
{
// Reset count
countEnd = false;
OsdManager.osdManager.bigMessage.text = messaggio + ": " + maxSec;
while (maxSec > minSec)
{
yield return new WaitForSeconds(1);
maxSec--;
OsdManager.osManager.bigMessage.text = messagio + ": " + maxSec;
}
// Notify that we are finished
countEnd = true;
}
}
}
// Other class using the CountDown class
using CD;
public class TestClass: MonoBehaviour
{
private CountDown test1;
void Awake()
{
test1 = new CountDown();
test1.StartCD("Testint", 5, 0);
}
void Update()
{
if (test1.countEnd)
{
print("Countdown ended");
}
else
{
print("Countdown still counting");
}
}
}
I had to change from “Private Void” to “Private IEnumerator” because of an error: cannot be an iterator block because `void’ is not an iterator interface type
after that I did call that method but now I have this other error
You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
I’m guessing the StartCoRoutine is called from the WaitForSeconds function. You can replace that line by
“System.Threading.Thread.Sleep(1000);” and see if it works then.
yield return new WaitForSeconds(1);
// becomes
System.Threading.Thread.Sleep(1000);
I can’t help out much more without actually having Unity here to test things, sorry.
MonoBehaviors can only exist if they are on a gameobject.
So instead of instantiating with new, try adding it to your existing gameobject like this:
// CountDown class in CD namespace
namespace CD
{
public class CountDown: MonoBehaviour
{
public bool countEnd = false;
private void StartCD(string messaggio, int maxSec, int minSec)
{
// Reset count
countEnd = false;
OsdManager.osdManager.bigMessage.text = messaggio + ": " + maxSec;
while (maxSec > minSec)
{
yield return new WaitForSeconds(1);
maxSec--;
OsdManager.osManager.bigMessage.text = messagio + ": " + maxSec;
}
// Notify that we are finished
countEnd = true;
}
}
}
// Other class using the CountDown class
using CD;
public class TestClass: MonoBehaviour
{
private CountDown test1;
void Awake()
{
test1 = this.gameObject.AddComponent<CountDown>();
test1.StartCD("Testint", 5, 0);
}
void Update()
{
if (test1.countEnd)
{
print("Countdown ended");
}
else
{
print("Countdown still counting");
}
}
}
It’s a pretty good habit to be in, though. If your code is in your own namespace then you don’t need to worry about whether or not your class names might collide with existing classes.
And if anyone comes across this and is considering writing code for an Asset Store package (or any code you plan to distribute to other Unity devs): If you don’t use namespaces on your project I curse you and your children for seven generations.