I’m creating a viewer where I want the user to be able to switch between two different scenarios with GUI-buttons, and I’ve been able to create a function that does this, however in my code I need to target each and every mesh and turn it on/off. Since I got hundreds of meshes it will not be a beautiful script.
I’m wondering if I can get a good suggestion on how to make this work better, for example I would love to be able to set visibility on all meshes within each FBX-file instead. Anyone knows how to make this happend?
My code (which I put on the main camera):
function UltimateOff () {
var ny_e4 = GameObject.Find("terrang_ny_01");
var provisorisk_e4 = GameObject.Find("terrang_ny_02");
ny_e4.GetComponent(updateVis_mesh).TurnOff();
provisorisk_e4.GetComponent(updateVis_mesh).TurnOff();
}
function OnGUI () {
var ny_e4 = GameObject.Find("terrang_ny_01");
var provisorisk_e4 = GameObject.Find("terrang_ny_02");
// Make a background box
GUI.Box (Rect (10,10,200,400), "Meny");
if (GUI.Button (Rect (20,40,150,20), "Ny E4")) {
UltimateOff();
ny_e4.GetComponent(updateVis_mesh).TurnOn();
}
if (GUI.Button (Rect (20,60,150,20), "Provisorisk E4")) {
UltimateOff();
provisorisk_e4.GetComponent(updateVis_mesh).TurnOn();
}
}
give the objects tags eg: World 1 World 2 then use one script to turn visibility on and off based on their tags if more objects with tags are needed than just those 2 just add them and make suere they are added to the switch code hope this helps.
you can find info on the unity scripting refs.
1. How do I add the tags to all my objects? When I select a number of objects in the Hierarchy and change the tag-property in the Inspector, I only change the last mesh selected.
2. I seem to be unable to edit the renderer-state from outside the Game Object, my new “make all render false”-function looks like this:
function UltimateOff () {
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("ny_vag");
for (var go : GameObject in gos) {
go.renderer = false;
}
}
Ya, I can add a prefix on all meshes, like “worldA_” and “worldB_” when I export them from Max.
Can you point me in the right direction of how to create a loop where I get a list of all meshes that start with “worldA_”? I suspect I should use GetComponents in some smart way to achive this.
Usually it is a bad idea to look for objects based on a tag in real time since it would be slow if you have a lot of objects.
You might want to create built in arrays with your objects with a given tag in the Start() function and then access the arrays, which would be faster. Anyway, if I understand correclty what you are trying to do you might want to consider other approaches as well.
layers: assign objects to different layers and then just change the culling mask of your camera to render only the objecs that belong to a certain set of layers and not the rest.
You can also try grouping objects by assigning them to the same parent. Then if you set the scale of the parent to 0, I would expect these objects to be invisible? I haven’t tried this though and it might be bad for performance…
Thanks for the advice, I have not yet been able to make it work.
@callahan.44: I use *.js-files to script, not C#. Is there a way to get a list of all objects starting with “worldA_” with javascript or is it easier to translate my scripting to C#, which I’ve never used?
@ivkoni: The problem is still how I assign a layer to a large number of meshes in an easy way? The “scale 0”-trick sounds a bit ugly to me, but I’ll try it later if I can’t get this to work, thanks.