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;
}
}
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.