- Home /
Mouseover to use parent and all children?
I have created a simple script that will make objects turn green when hovered over and then back to normal color when mouse exit. I also made it so if selected, the object turns red and stays that way. My problem arises when I have an "object" that has multiple parts, so I combine it into a parent with children and then the script no longer works. Ideally, I would like for it to turn the whole item (parent and children) green when either are moused over.
Here is the script:
#pragma strict
var selected = false;
var initialColor : Color;
function Start () {
initialColor = renderer.material.color;
}
function Update(){
if (selected == true) {
renderer.material.color = Color.red;
}
}
function OnMouseOver() {
renderer.material.color = Color.green;
}
function OnMouseExit() {
renderer.material.color = initialColor;
}
function OnMouseDown() {
if (selected == false && ItemsRemaining.items > 0) { ItemsRemaining.items--; }
selected = true;
}
Answer by whydoidoit · Jun 20, 2012 at 05:23 PM
Let's presume this script is called Boxes:
function OnMouseOver() {
var scan = transform;
while(scan.parent.GetComponent.<Hazards>() != null)
scan = scan.parent;
for(var hazard : Hazards in scan.GetComponentsInChildren.<Hazards>())
hazard.renderer.material.color = Color.green;
}
The script I am using is called Hazards.js and the goal of this "game" is for the player to identify all hazards in a scene. So say I have a bucket with a mop in it, and although they are separate objects, I have child'ed the mop under the bucket. I would like for them both to glow when either are hovered over, which isn't happening, even with your recommended script. Thank you though, hopefully my more detailed description can better relay what I am trying to do.
Well this code will work with Hazard too :) I will edit the answer - use something similar in your other routines.
No you use it for all of them.
Let me explain what it does. You put this on all of them and it works it's way up to find the parent which is the last one that has a Hazard script - then is sets all of the items below it to green...
That code was a replacement for your On$$anonymous$$ouseOver() routine in Hazards
Is the parent also a Hazard?
Your answer
![](https://koobas.hobune.stream/wayback/20220613073039im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make a simple tree 1 Answer
Parenting Transforms, Must (still) Set Parent at 0,0,0 ? 1 Answer
how would I "child" an un-rendered object? 1 Answer
Affect child's child from main parent's script? 2 Answers
Child of Parent 1 Answer