Hello!
I hope anyone can help me with this one.
I have 3 gameobjects, they will be invisible (uncheck the mesh renderer int he inspector window), when the mouse enter (OnMouseEnter) in one of the gameobjects the another 2 will be visible (renderer.enabled = true), but this code is note working.
My full code: http://www.pasteit4me.com/2277010
The code:
for(int j=0; j<=36; j++)
{
if(gameObject.name == "Duplas" + i.ToString() + "_" + j.ToString())
{
if(GameObject.Find(i.ToString()).gameObject.renderer != null GameObject.Find(i.ToString()).gameObject.renderer != null)
{
GameObject.Find(i.ToString()).gameObject.renderer.enabled = true;
GameObject.Find(j.ToString()).gameObject.renderer.enabled = true;
}
else
Debug.Log("ELSE");
}
}
OK, your code doesn’t really match your request. You obviously have at least 36 objects in your scene. if not more. These object seem to have a very specific naming pattern though. You are not stating if the object has children, so I assume that is not a concern.
I would start off with a base, then use code to call that. So create a GameObject called GameController then attach a script to that called GameController. That script would just maintain a list of objects and add the script it needs on the fly. (ObjectController)
// GameController
private var Objects : Array;
function Start(){
Objects=Array();
var tempComponents = gameObject.GetComponentsInChildren(Transform);
for(var obj : Component in tempComponents){
var o=obj.gameObject;
if(o != gameObject){
Objects.Add(o);
if(!o.GetComponent("ObjectController")){
o.AddComponent("ObjectController");
}
o.GetComponent("ObjectController").ControllerScript=gameObject.GetComponent("GameController");
}
}
}
function doMouseEvent(object : Transform){
for(var obj : GameObject in Objects){
obj.renderer.enabled=true;
}
if(object)object.renderer.enabled=false;
}
// ObjectController
private var ControllerScript : GameController;
function OnMouseEnter(){
ControllerScript.doMouseEvent(transform);
}
function OnMouseExit(){
ControllerScript.doMouseEvent(null);
}
I tested it. This method gets rid of alot of naming conventions and stuff that you need to keep everything organized.
@BigMisterB My game objects that need to do the action (active the mesh renderer) don’t have any children, but the 2 gameobjects thats will be active is children of another gameobject.
I couldn’t use your code, I created a gameobject with the name “GameController”, then I put the 2 gameobjects inside (make then childrens), after I put your code inside the gameobject "GameController.
What I doing wrong? Thanks for all your help!
I founded what I did wrong! Thanks the code works!!!
To show the gameobjects is fine (renderer.enabled = true), but to vanish with the 2 game objects (renderer.enabled = false) only work with one of the gameobjects, can you help me again?