I was wondering how you would manage clicking many objects (just like in a escape click game) and whether you would have an individual script for each and every object clicked or whether you could manage this in one script simply with a tag system? I looked up documentation for OnMouseDown and CompareTag:
CompareTag does not seem to be able to used without a collider so I am not sure how I would check the tag with a mouse click. Would something like the following actually work?
void OnMouseDown ()
{
if (gameObject.tag == "objectBlue")
{
picked += 1;
Debug.Log ("tag");
}
}
I want to use this for either a board game or escape clicking game where I would need dozens of decisions (if statements) to determine what happens when each object is clicked.
Hello @PolymathicIndustries, I am quite new to Unity, but I will try to answer as best as I can your questions
(1) - you would have an individual script for each and every object clicked or whether you could manage this in one script simply with a tag system?
Both are possible, but if you wanted to do the second method you would probably need to use Unity Input System instead, because the OnMouseDown() function is called from the GameObject that has been clicked. You would use something like Input.GetMouseButtonDown(int button) and use the mouse position on the screen and a ray to find the GameObject that has been clicked.
(2) - CompareTag does not seem to be able to used without a collider so I am not sure how I would check the tag with a mouse click. Would something like the following actually work?
I am assuming you put the OnMouseDown() inside a Script that is on the GameObject itself. In this case, your script should work. Also, I do not think the CompareTag() method needs a collider, but the OnMouseDown() does.
(3) - I want to use this for either a board game or escape clicking game where I would need dozens of decisions (if statements) to determine what happens when each object is clicked.
I do not know what type of board game you are trying to make, but I’ll say if you have a lot of GameObjects in your scene you might want to have one GameObject with a script that manages the interactions. But if you do not have that many gameobjects, using individuals script with the OnMouseDown() function is probably preferable. I hope this helps!
@MerryLeo, can you tell me why the following does not work? I’d appreciate a quick look.
if (Input.GetMouseButtonDown(0))
{
Ray ray = greenCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.collider.gameObject.tag == "Green Crane")
{
Debug.Log("We are green!");
}
}
}