- Home /
spawn and destroy with toggle button?
hey! i ve got this level editor where i toggle on and off different objects using the toggle button component. so all objects are in my scene and is just activate/deactivate them by using a dynamic setactive bool.
somehow i think thats stupid to have them all already in the scene. so i want to instantiate them. but how can i destroy them on the same button using a toggle button?
my idea is that when i for example want to spawn a cube, i have a green button says "spawn cube" and when i press this button it instantiates the cube and the green button turns into a red button (which can be done with a toggle element) and says "destroy cube"
how can i do this?
Answer by MasterLG · Jun 21, 2016 at 06:34 AM
Are you using the newer, more-in-the-editor style of UI, or the older GUI.Toggle sort of stuff?
For the aesthetic (text and colour) component:
In the case of the newer style, you would need a reference to the Toggle script on the UI element and the relevant components (the Toggle GameObject's Toggle script for access to the ColorBlock where you can set the normalColor as necessary, the Toggle GameObject's child Label to set the text). The code for this would look something like:
// toggle is the Toggle script from the Toggle GameObject
// toggleText is the Text script from the Toggle's child Label
public void ToggleCubeUIElement(bool currentValue) {
if (currentValue) {
// toggled to ON, prepare UI for turning OFF
toggle.colors.normalColor = Color.red;
toggleText.text = "destroy cube"
} else {
// toggled to OFF, prepare UI for turning ON
toggle.colors.normalColor = Color.red;
toggleText.text = "spawn cube"
}
}
You'll need to ask someone else for how to do it for the older style in terms of colourizing, but the principles are more-or-less the same (reacting to the state change).
For the functional component:
In both styles, you're addressing some change to a bool value and you'd do well to set up a method that simply takes the updated value (e.g. when the toggle is flipped). This also helps break up your code a little which may make management a bit easier.
From here, the method would look something along the lines of:
// cubePrefab is a prefab or GameObject to create
// cube (GameObject) is an instance of the cube
// position is a Vector3 to create the cube at
public void ToggleCube(bool currentValue) {
if (currentValue) {
// toggled to ON, spawn cube
cube = (GameObject) Instantiate(cubePrefab, position, Quaternion.identity);
} else {
// toggled to OFF, destroy cube
Destroy(cube);
}
}
My apologies if there are any code errors as this was written so far mostly off of the top of my head.
Is this what you were looking for?
right, i was talking about the new ugui, sorry. thanks! that will help me a lot :) the script above will be placed on the toggle button or on the on change value event field?
$$anonymous$$ore-or-less, yeah.
EDIT: really bad misread, sorry.
The script (or scripts) containing those methods can be placed on any GameObject (pick somewhere sensible). From there, you navigate to the Toggle script and then the On Value Changed (Boolean) will ask for listeners. Press the + sign to add a listener, then select the GameObject holding onto the script, and then select the script and method to do it.
Hooking up the UI element to the listener will look something like this: http://i.imgur.com/O0H97Wg.png
Your answer
