- Home /
"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!
the ones which you are comparing the name with, are they all the same.
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.
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)
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 = "";
}
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.
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 :(
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.
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.