The following script is supposed to allow the player to select/deselect an object by clicking the left mouse button.
(to be precise it enables/Sets to active a child object that has a projector component attached)
Im not getting any compiler errors but the script just doesn’t work. When I click on the units/characters in game I get the correct debug message (“Found Unit !”) but the child object is not getting activated ?
using UnityEngine;
using System.Collections;
public class Mouse : MonoBehaviour
{
RaycastHit hit;
public static GameObject CurrentlySelectedUnit;
public GameObject Target;
private Vector3 mouseDownPoint;
void Awake()
{
mouseDownPoint = Vector3.zero;
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, Mathf.Infinity))
{
// Store point(co-ordinates) at left mouse button down
if(Input.GetMouseButtonDown(0))
{
mouseDownPoint = hit.point;
}
if(hit.collider.name == "TerrainMain")
{
// Instantiate Target when user clicks the Left mouse button
if(Input.GetMouseButtonDown(1))
{
GameObject TargetObj = Instantiate(Target, hit.point, Quaternion.identity) as GameObject;
TargetObj.name = "Target Instantiated";
}
else if(Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
DeselectGameobjectIfSelected();
} // End of the Terrain
else
{
// Hitting Other Objects
if(Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
{
// Is the User Hitting a Unit?
if(hit.collider.transform.FindChild("Selected"))
{
// Found a Unit we can Select !
Debug.Log("Found Unit !");
// Are we Selecting a Different Object?
if(CurrentlySelectedUnit != hit.collider.gameObject)
{
// Activate the Selector
GameObject SelectedObj = hit.collider.transform.FindChild("Selected").gameObject;
SelectedObj.SetActive(true);
// Deactivate the Currently Selected Objects Selector
if(CurrentlySelectedUnit != null)
CurrentlySelectedUnit.transform.FindChild("Selected").gameObject.SetActive(false);
// Replace Currently Selected Unit
CurrentlySelectedUnit = hit.collider.gameObject;
}
} else
{
// If this Object is not a Unit
DeselectGameobjectIfSelected();
}
}
}
}
else
{
if(Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
DeselectGameobjectIfSelected();
}
Debug.DrawRay(ray.origin, ray.direction * Mathf.Infinity, Color.yellow);
}
#region helper functions
public bool DidUserClickLeftMouse(Vector3 hitPoint)
{
float clickZone = 50.0f;
if(
(mouseDownPoint.x < hitPoint.x + clickZone && mouseDownPoint.x > hitPoint.x - clickZone) &&
(mouseDownPoint.y < hitPoint.y + clickZone && mouseDownPoint.y > hitPoint.y - clickZone) &&
(mouseDownPoint.z < hitPoint.z + clickZone && mouseDownPoint.z > hitPoint.z - clickZone)
)
return true; else return false;
}
//deselcts gameobject if selected
public static void DeselectGameobjectIfSelected()
{
if(CurrentlySelectedUnit != null)
{
CurrentlySelectedUnit.transform.FindChild("Selected").gameObject.SetActive(false);
CurrentlySelectedUnit = null;
}
}
#endregion
}
Any ideas as to why the child object is not being SetActive(true) ?