Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Dubious-Drewski · Jun 21, 2013 at 05:54 AM · referenceself

How do I access the object the script is attached to?

This one has to be simple, but I can't find the words to Google this.

I have a script attached to an object in Unity. From that script, how do I access the object's name or other properties? I've read this page, but it doesn't seem to address my question.

I've tried Gameobject.name and this.name, but I can't make them work.

Comment
Add comment · Show 4
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 robertbu · Jun 21, 2013 at 05:58 AM 0
Share

Use 'gameObject'...'gameObject.name'...'gameObject.tag'. See the reference page for GameObject for all the properties you can access.

avatar image Dubious-Drewski · Jun 21, 2013 at 06:14 AM 0
Share

Hmm. This is my code:

if(gameObject.name="RightFeeler"){ Debug.Log ("Right feeler collided"); }

And this is the error: Cannot implicitly convert type 'string' to 'bool'

I don't know what that means. Any ideas?

avatar image robertbu · Jun 21, 2013 at 06:18 AM 1
Share

Comparison is '==', so it should be:

 if (gameObject.name == "RightFeeler") {
     Debug.Log("Right feeler collided");
 }
avatar image AlucardJay · Jun 21, 2013 at 06:20 AM 0
Share

Incorrect operator in your conditional

== , not = (is equal to, not equals)

 if ( gameObject.name == "RightFeeler" )

what do people have against using spaces, there is no optimization in not using spaces, and makes code so much easier to read

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by unibotai · Jun 21, 2013 at 06:19 AM

You attatch the script to the object then use the inspector tab in unity. You can also edit the name of the object in inspector.alt text


screen shot 2013-06-20 at 11.01.36 pm.png (29.7 kB)
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
0

Answer by Dubious-Drewski · Jun 21, 2013 at 06:27 AM

Oh my God, I needed a '==' instead of a single '='. What a rookie mistake. Thank you, everyone. You're always so helpful.

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
0

Answer by bubzy · Jun 21, 2013 at 06:31 AM

according to your code here, you are accessing the name of the gameObject that you attach this script to, not to another gameObject

this wouldn't really work in a collision situation as you want to find out what your current gameObject collided with, it cannot collide with itself.

you may want to look up triggers too http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html

ive found this a good method for finding WHAT you have collided with.

     void OnTriggerEnter(Collider other) {
         if(other.gameObject.name == "RightFeeler")
         {
         Debug.Log("Right Feeler collided");
         Destroy(other.gameObject); //or whatever
         }
     }
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 Dubious-Drewski · Jun 21, 2013 at 07:07 AM 0
Share

bubzy: I have a reason for this strange setup.

I am making an insect. I have a body and two child objects acting as a left feeler and a right feeler. The feelers are not physical objects - they simply guide the body away from obstacles when they 'feel' (collide with) something.

So each feeler has a variable for if Collided (true/false), and the body (somehow) pays attention to this and responds to the input.

I don't yet know how to code this, but this is why I'm practicing.

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

17 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

Related Questions

NullReferenceException: Object reference not set to an instance of an object 1 Answer

References to script objects outside of the gameobject in a class that does not inherit from monobehaviour 2 Answers

The reference on this behavior is missing! Unity Bug 1 Answer

How to call a function from another script without referencing it? 1 Answer

Simple Script getting Errors. 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