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 /
avatar image
4
Question by aLucidWorld · May 05, 2010 at 12:04 AM · objectparenthierarchyname

getting the object that the running script is attached to

Sorry if this has been answered before but I'm not even sure how to word a search to find what I'm looking for.

Basically, I have 4 cubes on the screen, one is the player the other three are "enemies". Each enemy is running the same script since they will all behave the same way, but I'm trying to put into code how to get the name of the object that is currently running the script.

if I could write the code in english it would look like this

print(the name of the object in the hierarchy that is running this script);

Can anyone help?

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
11
Best Answer

Answer by qJake · May 05, 2010 at 12:59 AM

You're going to kick yourself when you find out. :P

print(gameObject.name);
Comment
Add comment · Show 5 · 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 Mike 3 · May 05, 2010 at 01:05 AM 0
Share

or just print(name);

avatar image qJake · May 05, 2010 at 01:30 AM 0
Share

Wouldn't that print the name of the component, not the game object?

avatar image Mike 3 · May 05, 2010 at 10:48 AM 0
Share

no - Component.name returns the GameObject name

avatar image aLucidWorld · May 06, 2010 at 05:46 AM 0
Share

thank you! that lower case 'g' I think is what was getting me

avatar image qJake · May 06, 2010 at 08:03 AM 1
Share

Well that's a program$$anonymous$$g concept right there... "GameObject" is the class name, and "gameObject" is the $$anonymous$$onoBehaviour field that points to a reference to "this" game object. Big difference. ;)

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

No one has followed this question yet.

Related Questions

Find object that enters trigger 1 Answer

inside a script is there a way to disconnect a child from its paretn in c# 1 Answer

Load Object(3d model) by name with script at scene 2 Answers

End game then an Object is close to another object? 0 Answers

Why do I have lots of 'One Shot Audio' objects in my hierarchy? 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