- Home /
Visibility based on Tags
Hi folks,
I'm looking at making different things visible (or not) based on Tags. The scene is a house from an isometric perspective which the player can freely navigate (think 'The Sims'). When viewing the ground floor, the upper floors (and their contents) are not displayed.
I started out with;
GameObject.FindWithTag("firstfloor").renderer.enabled = false;
But that didn't work so I tried;
GameObject.FindWithTag("firstfloor").SetActive(false);
Which Does work to set initially, but if I change it to a SetActive(boolean)
then it doesn't change when I set the boolean value.
Also, I'm not sure if SetActive is what I really want since I want the upper floors to remain active as there are characters wandering around up there and stuff still happening so I want it all to carry on doing it's thing.
So, the actual function I've got is;
var FloorStartLevel : boolean = false;
function MoveAround () {
if (Input.GetKeyUp("e") && FloorStartLevel == false) {
FloorStartLevel = true;
transform.Translate(0, 14, 0);
}
if (Input.GetKeyUp("q") && FloorStartLevel == true) {
FloorStartLevel = false;
transform.Translate(0, -14, 0);
}
GameObject.FindWithTag("firstfloor").SetActive(FloorStartLevel);
}
Any help at all in getting this to work would be greatly appreciated!
Answer by clunk47 · Dec 09, 2012 at 05:32 AM
I know C# better, so you'll need to check this out and look it up for UnityScript. You need to get an array of each tag.. Say you have two tags. first tag is firstfloor, second is secondfloor. Say you have 20 gameObjects in scene, half and half of each tag (10 firstfloor, 10 secondfoor)..
in C#, instead of GameObject
u'd use GameObject[]
[] means array in C#.
the parameter you need is FindGameObjectsWithTag
hence the plural Objects instead of Object
GameObject[] f1 = GameObject.FindGameObjectsWithTag("firstfloor");
GameObject[] f2 = GameObject.FindGameObjectsWithTag("secondfloor");
then you need to get EACh object in the arrays. Simplest way...
again, this is C#, but I'm using a foreach statement: just look up foreach javascript equivalent on google...
foreach(GameObject f in f1)
{
f.GetComponent<MeshRenderer>().enabled = false;
}
foreach(GameObject f in f2)
{
f.GetComponent<MeshRenderer>().enabled = true;
}
Note: to disable rendering of an object, use GetComponent(MeshRenderer).enabled for JS or GetComponent().enabled for C#
I really hope this helps you out a bit :D
not sure why but it edited out the characters for getcomponent for my C# example. For JS it would be something like gameObject.GetComponent($$anonymous$$eshRenderer).enabled = false;
format code by using the 10101 button in the edit window, or manually put 4 spaces before the line of code. I have edited your answer.
Edit : No Worries =]
Thanks man I am tired as hell and have no idea why I didn't use the code format field lol.
Answer by simplex409 · Dec 10, 2012 at 04:10 PM
Hi folks,
Thanks Clunk, using an array never even occurred to me!
Still getting an error though. I'm not at home at the moment so don't have it in front of me, but basically it hides one of the firstfloor objects and then says something about no entry in the array. I'll put a bit more detail when I get home later and can have a looksee.
The new code I've got (or at least something very close to it; again, I'm not at home and looking at the actual thing.) is;
var Floorlevel : GameObject[]; Floorlevel = GameObject.FindGameObjectsWithTag("firstfloor");
for (var i = 0; i = Floorlevel.length; i++) { Floorlevel[i].GetComponent(MeshRenderer).enabled = false; }
You need to have "i" be less than length.
for(var i = 0; i < Floorlevel.length; i++)
This is because "i" will be EACH INDEX in the array, and an array index starts with 0. So say your length is 10, the index of the last item (number 10) would be 9. for a 10 object array, the full index would be 0-9. And Length starts with 1. So "I" will be INDEX, therefore "I" = 0 in the beginning.
Thanks again Clunk, you rock
Though now I don't know whether to be relieved it's such an easy fix, or embarrassed that I made such a silly mistake!
Dude believe me. I've made far worse mistakes lol. It' all a part of the learning process :D If this works for you, it would be cool if you accept the answer so it no longer appears in Unanswered section :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613085148im_/https://answers.unity.com/themes/thub/images/avi.jpg)