Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Biztor · May 12, 2016 at 08:30 PM · classesmethods

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tyche10 · May 12, 2016 at 08:44 PM 1
Share

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};

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tyche10 · May 12, 2016 at 08:45 PM 0
Share

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

avatar image andrei2699 Tyche10 · May 12, 2016 at 08:53 PM 0
Share

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);


avatar image Biztor · May 13, 2016 at 09:05 AM 0
Share

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>();
     }



avatar image
0

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.

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tyche10 · May 13, 2016 at 08:36 AM 0
Share

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.

avatar image Biztor · May 13, 2016 at 08:43 AM 0
Share

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.

avatar image Tyche10 Biztor · May 13, 2016 at 08:48 AM 0
Share

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)

avatar image Biztor · May 13, 2016 at 08:54 AM 0
Share

But AddComponent(TYPE) isnt it?

Why I can pass it a type?

avatar image Tyche10 Biztor · May 13, 2016 at 08:57 AM 0
Share

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.

avatar image Biztor Tyche10 · May 13, 2016 at 09:30 AM 0
Share

void AddOneScript(T scriptType) where T : Component { gameObject.AddComponent(T)(); }

something like this?

Show more comments
Show more comments
avatar image Tyche10 Biztor · May 13, 2016 at 11:16 AM 1
Share

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;
         }
 
avatar image Biztor Tyche10 · May 13, 2016 at 11:36 AM 0
Share

yes, this is the simplest way, Thank you very much!!!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I change a function from another script? 1 Answer

How can I add functions via a scriptswizzard variable? 0 Answers

How to make a class that can be accessed from any script in a project. 2 Answers

When I call my script2 in script1 it makes script2 global? It stops me from accessing script2's methods at all. 0 Answers

Referencing uninstantiated GameObject within a class? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges