- Home /
Is there something like clicked.gameObject?
Is there something like clicked.gameObject? (like col.gameObject)
Because I want to check the tag of the gameObject I clicked and do something with it.
EDIT: added script:
I'm trying to make it so if clicked tag is chopstik then selected = true. I know I'm not doing right though. How would I do it Properly?
function Update() { if (Input.GetButtonDown("Fire1")) { var hit : RaycastHit; if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) { Debug.Log(hit.collider.tag); if(hit.collider.tag == "Chopstik") { selected = true; } }
if (Input.GetMouseButtonUp (0)) {
selected = false;
print("deselected");
}
}
}
Answer by Mike 3 · Jul 20, 2010 at 08:09 PM
This will print the tag of the last clicked object:
function Update()
{
if (Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
{
Debug.Log(hit.collider.tag);
}
}
}
How would I set it to find the magnitued between on click and on release?
assign to a start var on mouse down (and set a bool to say you hit something or not), then use that in the mouse up if you hit something else to get the distance
How would I get distance? everything else I know. Also how come with the new code my on mouse release thing stops working?
Vector3.Distance(start, end). Not sure about why your new code doesn't work. Perhaps try GetButtonUp("Fire") if you don't want it mapped specifically to left mouse
The code is like this? if (Input.GetButtonUp("Fire")) because it doesn't recognize my inout key.....
Answer by equalsequals · Jul 20, 2010 at 06:07 PM
Since your click events are handled via a collider, you can check the collider component's game object by Collider.gameObject property. That returns a reference to the Game Object in which your Collider component is attached to. From there, the Game Object has a public property GameObject.tag which will return the tag of that object.
something like this:
function OnMouseDown():void { var clickedTag:String = collider.gameObject.tag; }
EDIT:
What you're doing wrong above is tracking in the Update. You don't need to. All of the info you'll need can be well encapsulated in the OnMouseDown event.
function OnMouseDown():void { var clickedTag:String = gameObject.tag;
if(clickedTag == "Chopstik") selected = true;
}
Hope that helps.
==
That is javascript. You also don't need collider.gameObject, you can just use tag. Or just gameObject.tag.
what if I wanted to do an if statement? edited question with my scripting problem.
applied code along with a print to tell me if it works. never print "selected". It doesn't seem able to tell when you click it. No compilation errors though. Please Help!
Answer by loginpawan · Aug 27, 2012 at 08:20 AM
even thought i put the collider on imported model yet it not react with OnMouseOver() function my code is <
function Update () {
if (Input.GetButton ("Fire1") )
{
// isSelected = true; var clickedTag:String = gameObject.tag; if(clickedTag == "Melon") { selected = true;
Destroy (gameObject);
Debug.Log("Is selected "); }
} }
> Somebody please help me
Your answer
Follow this Question
Related Questions
View an array of the transform position/rotation of all game objects with a specified tag., 0 Answers
check if Go.tag is this instance of the object 1 Answer
Check if there is a child with a tag? (multiple children but each diff tag) 1 Answer
When all objects with a certain tag has been destroyed, load the next level! 3 Answers
Shooting Specific GameObject via Tag 0 Answers