UnityNinja Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

UnityNinja CommunityLog in

UnityNinja - Video Game Development Community, including Resources, Forums, Marketplace & More.


descriptionCode and workflow for hiding/unhiding unity elements EmptyCode and workflow for hiding/unhiding unity elements

more_horiz
Hi Everyone!

I hope I am posting in the correct section. I know that it is possible to write a custom code in Unity so I have the following questions:
1. Imagine a house model in Unity. Would it be possible to have a code which helps to hide/unhide certain objects? For example, letter "W" would hide/unhide all windows, letter "C"  would hide/unhide all columns etc.
2. If it would be possible to develop a code for that, what would be the workflow? How would Unity know what is window and what is door?
3. Taking one step further.. Would it be possible to have a code that unhides the next step of the project. For example, the first step would be building foundations. Would it be possible to have a code that would unhide the next step, say, 1st floor floor element, with a klick of a keyboard key? And then with the same key unhide the next step which might be 1st floor walls. And would it be possible with another key go backwards?
4. If such code would be possible, what do you think would be the workflow? How would Unity know which element is in which step?

I am keen to see if this is possible!

descriptionCode and workflow for hiding/unhiding unity elements EmptyRe: Code and workflow for hiding/unhiding unity elements

more_horiz
Firstly you are looking at it in a linear view, when you say how would unity classify what object is what.. you are over thinking things. The engine will precisely follow the commands given, therefore it does not have a brain to interpret.. So In terms of toggling a gameobject's renderer, you have to set the gameobject's renderer.enabled property to either true or false, something like this:

Code:

function Update() {
 
    if (Input.GetKeyDown(KeyCode.W)) {
        // show (in this case windows)
        renderer.enabled = true;
    }
 
    if (Input.GetKeyDown(KeyCode.E)) {
        // hide Window with keypress E
        renderer.enabled = false;
    }
}

Now this code would be implemented onto the gameobject you would like to have this toggle effect on, however if you wanted to toggle the windows with only "W" you could do this:

Code:

function Update() {
 
    if (Input.GetKeyDown(KeyCode.W)) {
        // (toggle visibility:)
        renderer.enabled = !renderer.enabled;
    }
}


However like you mentioned the columns, I am assuming there are multiple columns, therefore you must create a parent column and make the rest a child of the parent and use this code to toggle all the columns at once:

Code:

function Update() {
    if (Input.GetKeyDown(KeyCode.C)) {
        ToggleVisibility();
    }
}
 
function ToggleVisibility() {
    // toggles the visibility of this column and all it's children
    var renderers = gameObject.GetComponentsInChildren.();
    for (var r : Renderer in renderers) {
        r.enabled = !r.enabled;
    }
}

descriptionCode and workflow for hiding/unhiding unity elements EmptyRe: Code and workflow for hiding/unhiding unity elements

more_horiz
Hey MasterNinjaBoy,

Thank you so much for your input!

The model would be imported from 3DS Max so I will not have an option to create columns my self. All the model elements would be imported from 3DS Max. Does it mean that I would have to click on every single element and assign the proper toggle visibility code?

How about the question 3? Do you have an opinion on that?

descriptionCode and workflow for hiding/unhiding unity elements EmptyRe: Code and workflow for hiding/unhiding unity elements

more_horiz
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply