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 jellyAM · Dec 20, 2012 at 04:01 PM · childrentogglevisibilityof

how do you toggle the visibility of child objects with a mouse over?

im currently working on an interactive map for a local museum that needs to have the ability to mouse over dots that represent various shipwrecks(which display text when moused over), me not being a very coder, don't really know how to solve this problem. i do have a script that can change material colors on a mouse over but i need to toggle the visibility from the off state to the on state for a 3d text child object, then back to the off state when the mouse cursor leaves, how would i go about doing this?

here is the code i've written so far in C sharp and in boo

 //the c sharp script
 using UnityEngine;
 using System.Collections;
 
 public class cursorScript : MonoBehaviour 
 {
     //public Component thing;
     
     void Start () 
     {
          renderer.material.color = Color.blue;
     }
     
     
     public void OnMouseEnter()// put the text visibility stuff right here
     {    
         renderer.material.color = Color.yellow;
          
         
         //thing = GetComponentsInChildren(Renderer);
     }
     
     public void OnMouseExit()
     {
        renderesr.material.color = Color.blue;
     }
     
 }






//the boo script import UnityEngine

class cursorScript (MonoBehaviour):

 //thing = gameObject.GetComponentInChildren(Renderer)
 
 
 def Start ():
     renderer.material.color = Color.blue
      

      
 def    OnMouseEnter()://3d test visibility stuff goes here
     renderer.material.color = Color.yellow
     
     
 def    OnMouseExit():
     renderer.material.color = Color.blue
Comment
Add comment · Show 2
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 jellyAM · Dec 21, 2012 at 12:58 AM 0
Share

i have tried that, the problem is that i need renderer.enabled = true to work a child object in a heirarchy and not affect the parent object at all

avatar image cagezero · Dec 21, 2012 at 03:56 AM 0
Share

Please convert this post to a comment on the answer it addresses. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by cagezero · Dec 20, 2012 at 10:31 PM

This example will find a renderer component somewhere in the hierarchy and allow you to toggle its state.

 public class CursorScript : MonoBehaviour  {
     
     Renderer currentRenderer;
     
     void Awake() {
         
         //This assumes that the renderer is active on load
         currentRenderer = GetComponentInChildren<Renderer>();
         currentRenderer.enabled = false;
     }
     
     public void OnMouseEnter() {  
            currentRenderer.enabled = true;
     }
 
     public void OnMouseExit() {
            currentRenderer.enabled = false;
     }
 }

--EDIT--

The easiest way to enable/disable all or a subset of the children would be to attach the CursorScript to each of the children you want to interact with. That would bypass the need for keeping track of children altogether. If for some reason that is not an option you can enable/disable all the children by caching all of the child Renderers instead of just the one.

 public class CursorScript2 : MonoBehaviour  {
 
     Renderer[] currentRenderers;
 
     void Awake() {
 
        //This assumes that the renderer is active on load
        currentRenderers = GetComponentsInChildren<Renderer>(true);
        SetRendererEnabled(false);
     }
 
     public void OnMouseEnter() {  
         SetRendererEnabled(true);
     }
 
     public void OnMouseExit() {
         SetRendererEnabled(false);
     }
     
     private void SetRendererEnabled(bool enableRenderer) {
         
         for(int x = 0; x < currentRenderers.Length; x++)
             currentRenderers[x].enabled = enableRenderer;
     }
     
 }



If you need to toggle only a subset you will need to keep track of which children need to be flipped. I would probably do this by attaching a MonoBehaviour (let's give it an arbitrary name like "RendererController" ^_^) to each child that can be toggled. You could then convert the code above to store references to RendererController scripts instead of Renderers. Each RendererController can then determine for itself if it should enable/disable its Renderer based on whatever criteria you wish. If you need help with this just let me know.

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 jellyAM · Dec 26, 2012 at 02:20 PM 0
Share

thanks for your help =) by the way how would i go about indicating if all the children's visibility need to be switched or just a subset?

avatar image cagezero · Dec 26, 2012 at 05:14 PM 0
Share

Glad to help! I have edited the answer to include info about dealing with multiple children.

avatar image cagezero · May 09, 2013 at 03:55 PM 0
Share

I hope this was useful to you. If you consider this question resolved, please consider marking the answer as correct.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Toggle Visibility 1 Answer

How to disable multiple renderer in gameobjects children 1 Answer

Disappearing GUI 2 Answers

Show and Hide a prefab or GameObject 10 Answers

Visbility keyframes in animation 1 Answer


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