- Home /
Selecting a Game Object with a mouse click on it.
Hello, I'm making a simple RTS game and I need to implement a selecting feature. In fact mu script works very well but the problem is that I also instantiate prefabs. And because these prefabs are buildings on a moving planet, I set the parent of those instantiated prefabs to the Planet they're build on, so they remain on it when the Planet moves.
However, when I do this, and try to select the building, it is the Planet that is selected, and after some testing it would seem that the reason of that is because the building is the child of the planet.
So here's my question; is it possible to make this selecting script select the building and not the Planet is is built on, and if not, is there any way to make the position of the building relative to the Planet unchanged when the Planet moves without making the building a child of the Planet.
The selection is made with OnMouseUp().
--EDIT-- As suggested tried to do with Raycast, here's the code: using UnityEngine; using System.Collections;
public class SelectingMicros : MonoBehaviour {
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo) && hitInfo.transform.tag == "Construction")
{
print ("It's working");
}
}
}
}
This doesn't work when the building is the child of the planet. I click on the building but the message doesn't appear, while if I put the tag of the Planet to "Construction" as well, it works but selects the Planet, not the building.
Trying to be less verbose and more specific in your questions.
Answer by Sisso · Mar 06, 2013 at 02:31 PM
Given your question "is it possible", almost everything is possible. Probably your are doing a basic error.Generally input selection there is no influence about your objects positions and hierarchy. Create some prototypes and take a look again in documentation:
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
EDIT: Added more info
raddry, try to put more debugs to know where exactly it is failing. Like:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse is down");
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if (hitInfo.transform.gameObject.tag == "Construction")
{
Debug.Log ("It's working!");
} else {
Debug.Log ("nopz");
}
} else {
Debug.Log("No hit");
}
Debug.Log("Mouse is down");
}
}
Remember that raycast hit only objects with Colliders, if I have a child with collider, the child.transform will be referenced in RaycastHit.
Example: Given GameObject HQ that have 2 children, Model with a renderer and Bound with Collider, I will got the Bound.
HQ (tag=construction)
|- Model (Renderer)
\- Bound (BoxCollider)
If I raycast this, I will got the Bound object. You can verify parent tag:
var hitConstruction = hitInfo.transform.parent.gameObject.tag == "Construction"
Thanks for the suggestion. As I said I was using On$$anonymous$$ouseUp and I can assure you that the hierarchy seems to cause problem about that. Since I can select the building no problem when it's not the child of the Planet. Will try to make the selection system based on raycast then.
It doesn't seem to be working but now I get why. When the raycast hits the building, it acts as if it hit the collider of the Planet because the planet is the Parent. I think it's not possible to make them as separate colliders here.
It is ok to have a hierarchy where each object has its own colliders. Are you using any layer? Something different if you change raycastHint.transform per raycastHint.collider?
Working! First when I put collider ins$$anonymous$$d of transform, the debug indicated that the House$$anonymous$$esh was hit ( House$$anonymous$$esh is contained in the actually instantiated object). But it still said "nopz", and I realized that I had to tag the House$$anonymous$$esh for construction to make it work. Perfect now thanks for help.
Answer by freeclup · May 21, 2013 at 11:46 AM
// this code show nameobject with click
if (Input.GetMouseButtonDown(0))
{
//empty RaycastHit object which raycast puts the hit details into
var hit : RaycastHit;
//ray shooting out of the camera from where the mouse is
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit))
{
//print out the name if the raycast hits something
Debug.Log(hit.collider.name);
//freeclup= hit.collider.name;
}
}
Answer by elblogdelbeto · Jun 23, 2018 at 01:07 AM
better and easy way, just add box collider and a script with this event to your game object:
OnMouseUpAsButton() { }
Your answer
![](https://koobas.hobune.stream/wayback/20220613093927im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make a simple tree 1 Answer
Child selection forces parent selection 1 Answer
Controlling GameObject selection across Parent/Child 1 Answer
Calling a function of another object's child? 1 Answer
Pick up toy and attach to hand 1 Answer