- Home /
Problem is outdated
MouseClick and gameObject
Hi, I want to retrieve the name of this gameObject on my scene when I click on an object in order to execute an animation but I can not do it. My click return nothing.
Code :
// Builds a ray from camera point of view to the mouse position
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Casts the ray and get the first game object hit
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
return hit.transform.gameObject;
else
return null;
Do you have an idea of the problem ? I attach this script on my gameObject (a Door)
Thanks
@Gael51 You can try using http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseUpAsButton>On$$anonymous$$ouseUpAsButton() if the door has a collider. Add that to the door, you will need the door to then pass the info from that function to wherever you need it
function On$$anonymous$$ouseUpAsButton()
{
animation$$anonymous$$oderator.GetComponent($$anonymous$$yAnimations).PlayAnimation(this.gameObject);
}
or however you are using the "hit" gameObject info.
are you sure you set up your layer mask properly? Did you assign's it's value manually by using a bit-wise statement, or did you make it a public variable and set the mask in the inspector?
Excuse me for being late but I solved my problem. $$anonymous$$y object was. Fbx that I mattered and properties was not correct
So i guess there's no answer to this question...
I'm going to close it since it seems to be solved.
Answer by Gael51 · May 09, 2012 at 08:43 AM
I simplified my code :
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Casts the ray and get the first game object hit
if (Physics.Raycast(ray, out hit))
{
Debug.Log("This hit at " + hit.point );
return hit.transform.gameObject;
}
else
{
Debug.Log("null");
return null;
}
But nothing. The variable 'ray' is correctly implemented. I noticed that the variable returned a lightMapCoord NullException
Follow this Question
Related Questions
selected object in array lost in translation 0 Answers
make object disappear after being thrown when mouse clicked anywhere ? 1 Answer
How to make when object is pressed, object2 does something? 0 Answers
Detect UI Panel click 2 Answers
Script for play audioclip on every object in the game by mouse click 0 Answers