How can I do a method that return a class?
I try this
public T test<T>()
{
switch((int)typeBuilding) // Enumerator
{
case 0:
return Wall; // class
break;
case 1:
return House; //class
break;
}
}
GameObject objeto=(GameObject)Instantiate(object,mousePosition(),Quaternion.identity);
objeto.AddComponent<test()>();
But dont works, How can I do it? thanks
Btw, the good thing about an enum is that you can say
switch(typeBuilding) // Enumerator
{
case BuildingTypes.WALL:
return Wall; // class
case BuildingTypes.HOUSE:
return House; //class
}
when enum is declared as: public enum BuildingTypes { WALL, HOUSE};
Answer by andrei2699 · May 12, 2016 at 08:37 PM
You can try this :
case 0:
return typeof (Wall);
break;
case 1:
return typeof(House);
break;
To be clear, here you are returning the type of the class. You can return an instance of a class or the type of a class
It depends on what @Biztor wants to do. If he wants to check if an object is of a type from the enum, than he must return the enum element.
return BuildingType.WALL;
If he wants to create some kind of factory in which he clickes a button which sets the typeBuilding and, for example, at mouse click this function is called, than he must return the object of the building class and attach it to the new instantiated object.
return new Wall();
Or he can simply return the type of class.
return typeof(Wall);
Something like this;
public Component[] myScripts;
void AddAllScripts()
{
foreach (var item in myScripts)
{
AddOneScript(item);
}
}
void AddOneScript<T>(T scriptType) where T : Component
{
gameObject.AddComponent<T>();
}
Answer by Biztor · May 13, 2016 at 08:23 AM
@andrei2699 , @Tyche10 Thanks, I want to return a Class, not an instance. I have more issues.
public typeof ChooseScript()
{
switch((int)buildingType)
{
case 0:
return typeof(Wall);
break;
case 1:
return typeof(House);
break;
}
}
object.AddComponent<ChooseScript()>();
But I have errors, hoe can I do that?. thank you very much.
Like I said, you can only return a specific instance (new or existing) of a class or the type of a class, you cannot return 'a class'. If you return the type of a class (or an enum with the class names you wrote yourself) you know in the rest of your code which class you want to be talking to. $$anonymous$$aybe if you are more specific about what you want to do we can help you more.
I want to add a specific script.
Can I make an array of Classes? not instances. For organizating my scripts, and choose the correct type of class or component to add.
No, but you can for example make an array of enums which refer to the class names, which you can use afterwards to know which classes you want to use. You cannot attach a script to a class. You can however declare an instance of a script (or class) inside another class.
public class SomeClass: $$anonymous$$onobehaviour
{
private AnotherClass anotherClassInstance = new AnotherClass();
}
You should see the class itself as kind of a blueprint, that doesn't really exist if no specific instance of it exists that was built using the blueprint which is your class (an exception is a static class, but I don't think you want to use that)
Yes you use that to add a specific instance of a class as a component to a gameobject. You're not adding 'the class' with that. What you are doing with that function is creating an instance using the blueprint of the class you pass as a type, and attaching it to the gameobject.
void AddOneScript(T scriptType) where T : Component { gameObject.AddComponent(T)(); }
something like this?
Ok, so as far as I understand you want a method that can at runtime deter$$anonymous$$e which class you want to add to a certain gameobject. You could do that like this
void AddRightClassToGameObject<T>() where T : Component
{
gameObject.AddComponent<T>();
}
which will attach the component of type T to the same gameobject this script is attached to. Or you can do it like this
void AddRightClassToGameObject<T>(GameObject myGameObject) where T : Component
{
myGameObject.AddComponent<T>();
}
which allows you to pass the gameobject you want the component of type T to be attached to. To call it with the right type, all you have to do is for example
switch (typeBuilding)
{
case BuildingTypes.HOUSE:
AddRightClassToGameObject<House>();
break;
case BuildingTypes.WALL:
AddRightClassToGameObject<Wall>();
break;
}
In the above the scripts will be attached to the same gameobject this script is attached to. For attaching it to another gameobject that you pass as parameter it looks like
switch (typeBuilding)
{
case BuildingTypes.HOUSE:
AddRightClassToGameObject<House>(cube);
break;
case BuildingTypes.WALL:
AddRightClassToGameObject<Wall>(cube);
break;
}
yes, this is the simplest way, Thank you very much!!!
Your answer
