- Home /
Question by
AnthMurphy · Feb 14, 2017 at 06:28 AM ·
unity 5javascriptobjectsrenderer.enabledhiding
Hiding and showing multiple objects
How do I write a script that can cycle through hiding and showing of five objects. I've tried using GameObject.Find(Torso1) to find my objects and then renderer.enabled to hide and show it, not sure if i'm heading in the right direction. The script is attached to my camera.
At the moment my error message is "The name 'hide' does not denote a vaild type ('not found').
Any help would be much appreciated.
Thanks:)
var show : GameObject;
var hide : GameObject;
private var myInt: int = 1;
private var visible: boolean = true;
function Update() {
if (visible == true){
if (Input.GetKeyDown(KeyCode.D))
{
myInt++;
}
if (Input.GetKeyDown(KeyCode.A))
{
myInt--;
}
if (myInt == 1)
{
show = GameObject.Find(Torso1);
//show = GetComponent.<hide>();
show = renderer.enabled = true;
}
if (myInt == 2)
{
hide = GameObject.Find(Torso1);
hide = GetComponent.<hide>();
hide.enabled = false;
show = GameObject.Find(Torso2);
//show = GetComponent.<hide>();
show = renderer.enabled = true;
}
if (myInt == 3)
{
hide = GameObject.Find(Torso2);
hide = GetComponent.<hide>();
hide = renderer.enabled = false;
show = GameObject.Find(Torso3);
//show = GetComponent.<show>();
show = renderer.enabled = true;
}
}
}
Comment
Best Answer
Answer by nbarber20 · Feb 14, 2017 at 09:00 AM
This Should work.
Some Things to look up: https://unity3d.com/learn/tutorials/topics/scripting/arrays
var VisObjects : Transform[]; // This is an Array, think of it as a list.
private var SelectedInList: int = 0;
function Update () {
if(Input.GetKeyDown(KeyCode.D))
{
SelectedInList++;
if(SelectedInList>VisObjects.length-1){// go back to the beginning of list if i try and go above the last item.
SelectedInList =0;
}
ChangeVis();
}
if(Input.GetKeyDown(KeyCode.A))
{
SelectedInList--;
if(SelectedInList<0){ // go back to end of list if i try and go below the first item.
SelectedInList =VisObjects.length-1;
}
ChangeVis();
}
}
//runs through the entire list and makes everything on it invisible, then makes the single object you have selected visible.
function ChangeVis(){
for(var i:int; i<VisObjects.length;i++){
VisObjects[i].transform.GetComponent.<Renderer>().enabled =false;
}
VisObjects[SelectedInList].transform.GetComponent.<Renderer>().enabled =true;
}