Question by
MoonJaspers · Jan 31, 2017 at 06:55 AM ·
gameobjectraycastcanvasmouseclick
How to get gameobject of script with raycast?
i can't seem to get my head around this... I'm very new to raycast, so i apologize for any obvious mistakes.
so i have a script attached to a gameobject that shoots a raycast from screen with an if statement seeing if it hits the gameobject holding the script. there are more then one gameobject of this type. everytime i clicked on the gameobject it would work as normal but it would activate the code on all of the gameobjects. is there a way to get the gameobject itself?
here is a part of the code:
if (Physics.Raycast (ray, out hit)) {
if (isSelected != true) {
if (hit.collider.transform.gameObject.tag == "City") {
isSelected = true;
}
} else {
if (hit.collider.transform.gameObject.tag == "City") {
isSelected = false;
}
}
}
}
Transform CityCanvas = transform.Find ("CityCanvas");
if (isSelected != true) {
CityCanvas.GetComponent<Canvas>().enabled = false;
} else {
CityCanvas.GetComponent<Canvas>().enabled = true;
}
thanks in advance
Comment
you need to compare something they is unique to the object, not the tag, which is identical on all of them, if we're talking about Citys here. you could check if
if (hit.gameObject != gameObject)
return;