I have this function from an old Javascript I wrote. I want to reuse it in a new script written in C# (2nd script below). The script is assigned to the parent and its children. I need to tell the children that they are selected.
function OnMouseDown() {
if (selected == false && ItemsRemaining.items > 0) { ItemsRemaining.items--; }
selected = true;
var scan = transform;
while(scan.parent && scan.parent.GetComponent.<Hazard>() != null)
scan = scan.parent;
for(var hazard : Hazard in scan.GetComponentsInChildren.<Hazard>())
hazard.selected = true;
}
void OnMouseDown()
{
if(!ObjectSelected)
{
var scan = transform;
while(scan.parent && scan.parent.GetComponent<IdentifiableObject>() != null)
scan = scan.parent;
Transform[] allChildren = GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren)
child.ObjectSelected = true; //NOT WORKING
}
}
Given you are using the two different languages you are best of sending a message to the other script and passing the value.
SendMessage("Selected", true);
...
Child:
void Selected(bool selected)
{
ObjectSelected = selected;
}
When you use scripts in different languages it is hard to integrate them as they are compiled separately.
Here is what I have now. When the parent object is hovered, it highlights all the children. When a child is hovered, it highlights itself and the parent but not the other children. What am I missing, so that all children are highlighted when one child is mouseovered?
void OnMouseOver()
{
if(!ObjectSelected)
{
var scan = transform;
while(scan.parent && scan.parent.GetComponent() != null)
{
scan = scan.parent;
scan.renderer.material.color = hoverColor;
}
Transform[] allChildren = GetComponentsInChildren();
foreach (Transform child in allChildren)
child.renderer.material.color = hoverColor;
}
}
void OnMouseExit()
{
if(!ObjectSelected)
{
var scan = transform;
while(scan.parent && scan.parent.GetComponent() != null)
{
scan = scan.parent;
scan.renderer.material.color = currentColor;
}
Transform[] allChildren = GetComponentsInChildren();
foreach (Transform child in allChildren)
child.renderer.material.color = currentColor;
}
}
void OnMouseDown()
{
if(!ObjectSelected)
{
var scan = transform;
while(scan.parent && scan.parent.GetComponent() != null)
{
scan = scan.parent;
scan.GetComponent().ObjectSelected = true;
}
Transform[] allChildren = GetComponentsInChildren();
foreach (Transform child in allChildren)
child.GetComponent().ObjectSelected = true;
}
}