Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Stefan02 · Nov 06, 2017 at 06:59 PM · scripting problemgameobjectstringcomponent

C# Enable component on another GameObject using name of component

I spent many hours on finding an answer but no success. Thing is simple. After timeOut, a component on another gameobject will become active. It looks like this:

using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class TimedObjectEnableComponent : MonoBehaviour {
 
     public float timeOut = 1;
     public GameObject go;
     public string NameOfComp;
 
     void Awake()
     {
             Invoke("EnableComponent", timeOut);
     }
 
     void EnableComponent()
     {
         go.GetComponent<NameOfComp>().enabled = true;
     }
 }

But it result with error CS0118: TimedObjectEnableComponent.NameOfComp' is a field' but a `type' was expected. Any help would be appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MacDx · Nov 06, 2017 at 10:27 PM

Yes. The problem is exactly what the error describes. You are passing a string where you should be passing type instead. Type is a special class whose instances can represent any object's type and this object (the type one) is NOT the same thing as a string thus the error. The version of the GetComponent method that you are using expects a type, so what you need to do is convert your string into a valid type before passing it.

To convert your string into a type you will need to use a bit of reflection.

 void EnableComponent()
 {
     Type compType = Type.GetType(NameOfComp);
     go.GetComponent(compType).enabled = true;
 }

Keep in mind that GetType only works for classes in the same assembly. If your class is in another assembly you will also need to specify the name of that assembly. If your class is inside a namespace you will need to specify the namespace as well.

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 Stefan02 · Nov 07, 2017 at 08:02 PM 1
Share

It don't do the job. (Unity 2017.1) I'm getting new error, CS1061: Type UnityEngine.Component' does not contain a definition for enabled' and no extension method enabled' of type UnityEngine.Component' could be found. Are you missing an assembly reference?

Also type didn't worked so i modified it to System.Type

But thanks for response!

avatar image MacDx Stefan02 · Nov 07, 2017 at 08:42 PM 0
Share

Yup. $$anonymous$$y mistake I forgot that the field enabled is specific to the Behaviour class (which inherits from Component and is also the class which $$anonymous$$onoBehaviour derives from) but the GetComponent method returns an object of the class Component (who would've known). So just cast the result into a monobehaviour and you are good to go.

 Type compType = Type.GetType(NameOfComp);
 $$anonymous$$onoBehaviour comp = go.GetComponent(compType) as $$anonymous$$onoBehaviour;
 comp.enabled = true;


Also type didn't worked so i modified it to System.Type

Type like I wrote, and System.Type are exactly the same thing. It is only a matter of putting this line: using System; on top of your file to call the Type class implying the System namespace, or you could save that line and write it explicitly like you did.

Regards :)

avatar image Stefan02 MacDx · Nov 08, 2017 at 02:49 PM 0
Share

It actually works now! Thank you for saving my life!

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

196 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 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 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 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 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

Help! Script won't work. 1 Answer

The score/count only goes to 1 and not further. 0 Answers

Template object and script,GameObject scripts duplicated from a template game object 0 Answers

Can't change bool while accessing other script 2 Answers

how to move a gameobject based on a dice roll 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