OnMouseDown to activate gameobjects' child
I want to activate and deactivate a child of a gameobject by clicking on this gameobject with the mouse. When I start in game mode the child is not activated and when I click for the first time on the gameobject it is activated. Until now it's good, the problem comes next when I click again in the same gameobject and nothing happens. I was expecting the child to disappear but it continues there.
I'll put the code here so you can see. Maybe it's the functions of activeself that aren't working but this makes sense to me.
using UnityEngine;
using System.Collections;
public class ActivateGizmo : MonoBehaviour {
private GameObject child;
void Start()
{
child = gameObject.transform.GetChild (0).gameObject;
}
void OnMouseDown()
{
if (child.activeSelf == false)
{
child.SetActive (true);
} else if (child.activeSelf == true)
{
child.SetActive(false);
}
}
}
Answer by Suddoha · Sep 07, 2015 at 04:07 PM
The script itself works as you expect it to work. I just tested it.
However, it might be that the activated child covers the part of the parents collider that you're trying to click at. They raycast that OnMouseDown uses internally would be stopped at the child's collider in this case.
Yes thank you, that was the problem! I remove the collider from the child and now it works perfectly