- Home /
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
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
Please convert this post to a comment on the answer it addresses. :)
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.
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?
Glad to help! I have edited the answer to include info about dealing with multiple children.
I hope this was useful to you. If you consider this question resolved, please consider marking the answer as correct.
Your answer
Follow this Question
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