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 /
avatar image
2
Question by DrMrkev · Aug 10, 2016 at 03:21 PM · script.componentdrag-and-droptypeany

How to get ANY component type

Hello everyone, I'm doing a script in which you can drag and drop any type of component, this script disables him and after some time again enable. Unfortunately I have not found a way to get "ANY" type, certainly it will datatype but I do not know what type.

I imagine something like this

 public any dropped;
 dropped = this.gameObject.GetComponent<any>();
 dropped.SetActive(false);
 //After 5.Sec
 dropped.SetActive(true)
 //Repeat lines 3-5


could anyone help me?

ps. Sorry if some part of the text is unreadable or illogical. I am Czech, and with this text helped me a lot Google Translate.

-DrMrkev

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by LK84 · Aug 10, 2016 at 04:58 PM

I would go for generics. Also note that SetActive is for GameObjects, not Component. For Component you can use enabled, but only if the derive from MonoBehaviour (e.g. Scripts). For all others Components I don't know any way to disable them in the current Unity Version. So you might have to destroy and Add them. You could try this:

     IEnumerator genericMethod<T>(GameObject go) where T:Component
     {
         T dropped;
         dropped = go.GetComponent<T>();
         if (typeof(T).IsSubclassOf(typeof(MonoBehaviour)))
         {
             //Scripts
             (dropped as MonoBehaviour).enabled = false;
             yield return new WaitForSeconds(5);
             (dropped as MonoBehaviour).enabled = true;
         }
         else
         {
             //Other Components
             Destroy(dropped);
             yield return new WaitForSeconds(5);
             go.AddComponent<T>();
         }
 
     }

Comment
Add comment · 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
2

Answer by NoseKills · Aug 10, 2016 at 06:21 PM

I think you should describe what you want a bit more.

@LK84's answer is good but i would assume the generics might not suit your purposes since you are talking about dragging and dropping things. In generics you have to know the types compile time.

Like LK84 said, you don't need a Component to call SetActive().

You can get any Component with GetComponent(), but if the Component you get is a Transform for example, how would you disable or enable it?

Behaviours are 'components that can be enabled or disabled' as Unity docs say You can get any Behaviour by calling GetComponent() and use the enabled variable.

If you want to be even more specific, you can just get any 'script' with GetComponent()

Comment
Add comment · Show 1 · 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 DrMrkev · Aug 15, 2016 at 02:00 PM 0
Share

I will explain this in more detail. I have a fan that can blow players away. It works so that the player is in a trigger collider when it is inside its rigidbody a force in a direction away from the fan (local direction is converted into the global). These fans have a "FanController.cs" which the player blowing away. When this script to disable the fan will not respond to the players and not him puffing away. That's how I could make a temporary way he would have had (with the right ti$$anonymous$$g) pass. I would like the ti$$anonymous$$g to do another script because the script could also take care of the animation and particle effects, which could script FanController.cs do not (because he was at that moment inactive).

the ti$$anonymous$$g, I will use only the components created by me (C # scripts)

ps. using Unity 3D 5.0.0 version because it supports Windows XP are currently using ($$anonymous$$y computer is broken and not repaired, my friend lent me a very old netbook with an Intel Atom processor and 1GB of RA$$anonymous$$, I know it's bad but it's still better than nothing ).

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I can't use Rigidbody2D in my scripts, I can't find rigidbody2d in my script 3 Answers

Is there a type which can refer to both GameObject and MonoBehaviour? 1 Answer

2D Animation does not start 1 Answer

Error CS0425: Works in DLL but not in Unity 0 Answers

Button onClick based on image (sprite) on Button 1 Answer


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