x


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

 }
 }
more ▼

asked Jul 24 '12 at 05:52 PM

Stridulent gravatar image

Stridulent
4 4 5

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jul 24 '12 at 05:55 PM

whydoidoit gravatar image

whydoidoit
33k 11 23 98

I'm not using the scripts together. I'm wanting to re-use a feature from the *.js in a C# script.

Jul 24 '12 at 05:58 PM Stridulent

So you are dumping the JS?

In that case you want to get the component in the foreach loop

  child.GetComponent<ScriptNameGoesHere>().ObjectSelected = true
Jul 24 '12 at 06:05 PM whydoidoit

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.

Jul 24 '12 at 07:06 PM Stridulent
(comments are locked)
10|3000 characters needed characters left

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;
 }
 }
more ▼

answered Jul 24 '12 at 08:17 PM

Stridulent gravatar image

Stridulent
4 4 5

You want to do scan.GetComponentsInChildren() I think

Jul 24 '12 at 08:42 PM whydoidoit

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 " < > "?

Jul 26 '12 at 01:21 AM Stridulent
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4142
x1274
x418
x183

asked: Jul 24 '12 at 05:52 PM

Seen: 384 times

Last Updated: Jul 26 '12 at 03:33 AM