Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Der_Kevin · Jun 21, 2016 at 05:59 AM · instantiatedestroytoggletoggle button

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Der_Kevin · Jun 21, 2016 at 06:44 AM 0
Share

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?

avatar image MasterLG Der_Kevin · Jun 21, 2016 at 06:50 AM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

54 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

walk through an object once 0 Answers

Which is faster for a pool of objects, instantiating and adding to an array or loading pre-made objects to an array? 1 Answer

Could someone show me examples of how to use void OnDestroy() to instantiate an object? 2 Answers

Is there anyway to delete clones that are local variables? 1 Answer

Destroy and instantiate far objects 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges