I am trying to set-up a scene so that I can mouse click on the collison boxes of different objects to trigger unique events. My difficulty is making each object unique… I have been trying this with tags but it seems any object that I click on will be affected. My code (with some commented-out alternate attempts):
var lastUpdateWasAHit : boolean;
var lastHit : RaycastHit;
var EM : GameObject;
function Update ()
{
if (Input.GetButtonDown("Fire1")) // Detect hit on Fire1 command (left mouse btn)
{
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, Mathf.Infinity) lastUpdateWasAHit != true EM.tag == "EM")
{
//if (EM.tag == "EM")
//{
hit.collider.gameObject.audio.Play();
hit.collider.gameObject.renderer.material.color += Color.red;
lastHit = hit;
lastUpdateWasAHit = true;
print("was hit");
//}
}
if (Physics.Raycast(ray, hit, Mathf.Infinity) lastUpdateWasAHit != true EM.tag != "EM")
{
// nada
}
else if (lastUpdateWasAHit)
{
lastHit.collider.gameObject.renderer.material.color -= Color.red;
lastUpdateWasAHit = false;
}
}
}
Your code could use some cleaning up, but before diving into that, I think it would be helpful if you could describe in more detail the behavior you’re after.
What is the ‘EM’ game object exactly? What’s supposed to happen when you click on an object? Is the idea that whatever object was last clicked will be red while the rest will be some other color?
The changing color and play audio sounds are just for test purposes. Eventually, clicking on a particular object will bring up a unique menu describing that object. EM was for an electron microscope object. Clicking on it would bring up information (a text box) describing it. Another object in the scene is a light microscope… same thing - click on it and it displays information about it. This is obviously an educational program to learn about science tools in a fun 3D environment. I can click on the EM object and have it turn colors, play a sound, or even display the label I want. However, even using tags, I have not been able to assign the unique functions to just the particular object I click on. Thanks for the help.