- Home /
 
Manipulating booleans from parent to child (C#)
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
 
  }
  }
 
              Answer by whydoidoit · Jul 24, 2012 at 05:55 PM
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.
I'm not using the scripts together. I'm wanting to re-use a feature from the *.js in a C# script.
So you are dumping the JS?
In that case you want to get the component in the foreach loop
   child.GetComponent<ScriptNameGoesHere>().ObjectSelected = true
 
                 Thanks, that did the trick. I added the same to the "scan" variable so that if the child is selected, it can tell the parent to be selected too.
Answer by Stridulent · Jul 24, 2012 at 08:17 PM
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;
  }
  }
 
              Did not work in the spots that I thought you meant. Where were you thinking of putting that line? And do I attach the IdentifiableObject script in " < > "?
Your answer
 
             Follow this Question
Related Questions
How to move the parent object to a child of one of its children? 2 Answers
How to move the parent object to a child of one of its children? 1 Answer
Get Children of Child and Deactivate/Activate on Demand 1 Answer
transform.childCount == 0, but a child shows up in the hierarchy. How is this possible? 2 Answers
sending a message to child objects 2 Answers