• 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 /
This question was closed Oct 16, 2015 at 07:04 PM by theOtherHolmes for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by theOtherHolmes · Oct 15, 2015 at 10:57 AM · components

Any way to retrieve the *real* name of a script component?

According to the Unity API, Component.name always returns the name of the game object the component is attached to, not the actual component name. But what if I want the actual component name, such as the name of a script component, which -- unlike things like Transform or Rigidbody -- usually has a name that Unity won't automatically recognize? Is there anyway I get the name of the script that makes up the component?

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 meat5000 ♦ · Oct 15, 2015 at 11:43 AM 1
Share

I guess that would be the 'Type' rather than the name.

1 Reply

  • Sort: 
avatar image
1

Answer by Xarbrough · Oct 15, 2015 at 04:45 PM

 Debug.Log(this.GetType().ToString());

Note, that this will only work if "this" is not null. You can of course also put in a reference from outside of the script.

 PlayerSpawner m_Spawner;
 // get spawner somehow
 print(m_Spawner.GetType().ToString());

Why do you need this? If you want to retrieve a class name without having a runtime instance of the class, it gets a lot more complicated. I found a solid solution on the web, but it's far from trivial to create runtime instance by only using the string name of the class.

Comment
Add comment · Show 2 · 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 theOtherHolmes · Oct 15, 2015 at 06:09 PM 0
Share

Why do you need this?

Good question, the answer is complicated. The shortest way to describe it is that I am modifying a global notification system to be even more modular. $$anonymous$$ost notification systems have each script component register itself directly with the notifications manager. For reasons that would take a while to explain, I would rather have a single script component on each game object (I call the script “Distributor”) that registers with the notifications manager, and then Distributor knows how to distribute messages from the notifications manager to other script components on the same object, such as the $$anonymous$$ovement script, the Weapons script, the Inventory script, etc. But not every game object has the same components—some have only $$anonymous$$ovement, some have only Weapons, some do not have Inventory, etc., etc.

So in Distributor, I created a public Component[] scriptsToTrigger variable, and then I dragged the various relevant components ($$anonymous$$ovement, Weapons, etc.) into that array in the Inspector. When a message comes down from the notifications manager to the Distributor, it is a comma-delimited string, and the first item in the string is the name of the script component ($$anonymous$$ovement, Weapons, etc.) that should eventually receive the notification.

So I want to loop through the scriptsToTrigger array looking for a script component with the same name as the first item of the notification string. If there is a match, Distributor passes the notification string on to that script. And this is where my problem arose, because Component.name does not actually return the name of the component. I will try GetType when I get home tonight (unfortunately, Unity is not my day job . . .)

avatar image theOtherHolmes theOtherHolmes · Oct 16, 2015 at 07:04 PM 0
Share

GetType().ToString() did the job. Thanks.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Multiple Game Objects, different components 1 Answer

Documenting Unity classes/ components/ system 1 Answer

Infinite Loop while Removing Components 1 Answer

Unity editor bugging out 4 Answers

UNet bugs all over the place, fixed after re-building the Player prefab. 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