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 HalversonS · Apr 23, 2014 at 07:11 PM · collidercomparegameobject.name

"if(hit.collider.gameObject.name == gameObject.name)" only compares as true on the gameObject that first recieved the script

I have multiple gameObjects with this script on them. The only gameObject that properly changes the ToolTipText to what's set in the inspector is the gameObject that first received the script (I'm guessing some sort of timestamp is being used). All subsequent gameObjects to receive the script change the ToolTipText to "";

Am I not comparing object's correctly? Other methods I've tried give me a Cannot convert 'String' to 'UnityEngine.GameObject'

 var cam : Transform = gameObject.Find("CameraRight").transform;
 var ray = new Ray(cam.position, cam.forward);
 var hit : RaycastHit;
 
     if(Physics.Raycast (ray, hit, 500)) 
     {
 
          if (hit.collider.gameObject.name == gameObject.name)
              {
               meshTtt.GetComponent(TextMesh).text = ToolTipText    
                 }
         else
         {
             meshTtt.GetComponent(TextMesh).text = "";
         }

This happens if I compare tags as well...

 if (hit.collider.gameObject.tag == "isPlanePart")


Thanks for any help!

Comment
Add comment · Show 3
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 APenguin · Apr 23, 2014 at 07:32 PM 0
Share

the ones which you are comparing the name with, are they all the same.

avatar image kromenak · Apr 23, 2014 at 07:36 PM 1
Share

Where is this code located? Do you have it in an Update function or elsewhere? The way it's written, it seems that it should be running every frame (in Update) to constantly see if anything is hit.

I'm not totally sure what the problem is, but you might try to output hit.collider.gameObject.name to ensure that it is what you expect it would be. Especially when dealing with Rigidbodies and multi-part colliders, it can get confusing as to whether you have the "right" collider or not.

Even if the colliders are very simple, outputting the data to the console can help you check your assumptions.

avatar image HalversonS · Apr 23, 2014 at 09:04 PM 0
Share

Not all objects are the same. They have different names. Some use box colliders and some use mesh colliders.

Yes, this is in the update function.

I'm getting the error NullReferenceException: Object reference not set to an instance of an object Tooltips.Update () (at Assets/Scripts/Tooltips.js:57)

line 57 is- if (hit.collider.gameObject.name == gameObject.name)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by APenguin · Apr 23, 2014 at 09:29 PM

maybe change it to this:

 var cam : Transform = gameObject.Find("CameraRight").transform;
 var ray = new Ray(cam.position, cam.forward);
 var hit : RaycastHit;
  
     if(Physics.Raycast (ray, hit, 500)) 
     {
  
       if (hit.collider == gameObject.collider)
        {
         meshTtt.GetComponent(TextMesh).text = ToolTipText  
                }
         else
         {
            meshTtt.GetComponent(TextMesh).text = "";
         }
 
Comment
Add comment · Show 4 · 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 HalversonS · Apr 23, 2014 at 09:34 PM 0
Share

It yields the same results :P The first object that got the script works fine.. all others use the else statement. If I delete the script from the first object then the one that subsequently got it will work properly.

avatar image HalversonS · Apr 23, 2014 at 09:40 PM 0
Share

However, I am able to get it to output now. The gameObject it is hitting is indeed the correct one on each object I check. But for some reason it uses the else statement if I'm not looking at the first one :(

avatar image APenguin · Apr 23, 2014 at 09:58 PM 0
Share

maybe what you can try and do, is to make a selectedobj variable which is equal to the gameobject which has been hit. Then do an if statement saying that if the gameobject.transform.position is equal to the selectedobj then output that text that you want, else output no text.

avatar image HalversonS · Apr 23, 2014 at 10:09 PM 0
Share

Still behaving the same. I'm finding this quite odd. I'll work with that idea some more and see if I can't get it to function properly.

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

22 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Terrain with lots of objects 1 Answer

Ignore/Destroy then re-enable Mesh Colliders 3 Answers

C# Instantiating a Gameobject with a Flat Sphere Collider 1 Answer

Compare hit.collider.name 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