- Home /
How do I make a variable of type C# class?
I am trying to create a class whose constructor includes a variable of type C# class. Are there any packages/keywords/etc. that can allow me to do that? For my specific instance, simply declaring the variable of type will not work.
Answer by MaxGuernseyIII · Sep 03, 2017 at 02:59 AM
I think what you are asking is how to create a constructor for a class that allows you to initialize a variable of some other type.
// the class with the parameterized constructor..
public class HasConstructor
{
private ParameterType variable;
public HasConstructor(ParameterType parameter)
{
variable = parameter;
}
}
// the class you want to pass into the above...
public class ParameterType
{
}
// to create an instance of the first class...
new HasConstructor(new ParameterType());
Since I'm not really sure exactly what you are asking, though, I can't really be sure this is the answer to your question.
Thanks. I figured out an alternate method of solving my problem; I appreciate the answer though!
'Alternative method' oh dear that does make me feel good. Considering Unity is supposed to be newbie friendly I implore you to share your code so we can help you and make sure youre not going off on some tangent with code which use is for something completely different.