Why is Child Object not being set to Active (SetActive(true) on mouse click ? (Solved)

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) ?

If it’s not active when you use FindChild I’m pretty sure it won’t be found.
So if you can’t attach a public reference to it on this script then you’ll want to iterate through the transforms in the hit.collider.transform like so:

GameObject selectedChild;
foreach (Transform t in hit.collider.transform)
{
    if (t.name == "Selected")
    {
        selectedChild = t.gameObject;
        break;
    }
}

if (selectedChild != null)
//do stuff

Furthermore a quick look at the scripting documentation has nothing on FindChild, so you probably shouldn’t be using it or it was deprecated long ago.
Good luck!

EDIT:
Surprised it works, but I’m glad it does for you.

Ok… I’m officially an idiot !

It is working, what I didn’t realise is that by default projector components are set to disabled. Therefor I was actually selecting/deselecting the units… I just wasn’t get any visual confirmation of this as the projector never displayed the graphic.

Once I set the projector component to active/enabled and applied that to my prefabs everything works fine !