C#- How to get child objects to recognize being clicked and do correct action

Very new to scripting. Trying to get pieces of a character to glow when selected(clicked). My current script works when placed individually on the objects, however, I would like one script on one object: either the parent or camera (whatever is best for these situations). When I do this is does not sense the children being clicked upon at all(nothing happens), and my research isnt finding me that clear missing piece to the script.

So you are aware how I am getting the glow: it is the “glow” image effect and I am switching the material on my character from a texture with no alpha to one with.

My bare script:

using UnityEngine;
using System.Collections;

public class textureglow : MonoBehaviour {
	
	public Texture glowtexture;
	
void OnMouseDown() {
		 //changes begining texture with no alpha to a texture with for glow effect
		renderer.material.mainTexture = glowtexture;
		Debug.Log ("pressed");
    }

1 Answer

1

Well hopefully your character is the parent and all the limbs (I imagine that’s what your clicking) are the child of the object.

First thing is to get all the child objects:

Component[] childObjects = GetComponentsInChildren<"Parent Gameobject goes here">();

Now you have an array that holds all your objects. Then you can just check OnMouseHover() against any or all of the objects in the array.