- Home /
Cannot assign script to objects when class is within a Namespace
I have a namespace that is a collection of classes that all need to know about each other. I want to be able to assign these different classes to objects. However, when I go to Add Component the Foo script file does not show up as a choice.
I have read some other issues related to this and they all seem to be based on default parameters. But as you can see I am not using any default params. Is there some other reason that my classes are not showing up as available scripts to assign to objects?
namespace Foo
{
public enum mytype { Basic, Standard, Deluxe, Premium };
public class Owner : MonoBehaviour
{
private string _name;
private double _cash;
private double _networth;
public Owner()
{
_cash = 100000.00;
_networth = _cash;
}
public string name
{
get { return _name; }
set { _name = name; }
}
public double cash
{
get { return _cash; }
set { _cash = cash; }
}
public double networth
{
get { return _networth; }
set { _networth = networth; }
}
}
}
Can you post the namespace? I'm wondering if there's a conflict between namespace and class name.
Answer by VesuvianPrime · Dec 15, 2014 at 10:03 PM
Good catch on constructor.
MonoBehaviours didn't used to work in namespaces, but in Unity 4 they absolutely do.
Answer by Paulius-Liekis · Dec 15, 2014 at 08:36 PM
I think you're not supposed to use constructors for MonoBehaviours - you should use Awake and Start methods.
MonoBehaviours in namespaces might be not supported, I don't remmember exactly.
I commented out the Constructor. That made no difference. The namespace still does not show up in the scripts.
The class name and the namespace have completely different names.
I even tried commenting out the Enums as well.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174117im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Necessary to remove unused scripts from imported packages for deployment? 1 Answer
[C#]Adding a Sprite from a Resources.Load("Path") : How does it work ? 3 Answers
get all scripts attached to gameobject 2 Answers
How to keep a moving object at the height of the terrain? (script) 2 Answers
InstanceIDtoObject namespace? 1 Answer