Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Stridulent · Jul 24, 2012 at 05:52 PM · c#transformparentchildren

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
 
  }
  }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stridulent · Jul 24, 2012 at 05:58 PM 0
Share

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

avatar image whydoidoit · Jul 24, 2012 at 06:05 PM 1
Share

So you are dumping the JS?

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

   child.GetComponent<ScriptNameGoesHere>().ObjectSelected = true
avatar image Stridulent · Jul 24, 2012 at 07:06 PM 0
Share

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.

avatar image
0

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;
  }
  }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jul 24, 2012 at 08:42 PM 0
Share

You want to do scan.GetComponentsInChildren() I think

avatar image Stridulent · Jul 26, 2012 at 01:21 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges