Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ScottMcG · Oct 31, 2011 at 11:42 PM · scene viewonsceneguiselected

If I create GameObjects in OnSceneGUI, how can I destroy them when my control is no longer selected?

I have a GameObject / AI marker that creates + destroys prefabs during gameplay (Spawns a group of AI when camera is close, unspawns them when camera goes away). I want to view the spawned AI in Scene View when I select the parent marker (currently doing this with OnSceneGUI) but they get leaked when the marker is no longer selected. Can anyone suggest how/where to clean them up when the marker is not selected?

Comment
Add comment · Show 1
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 ScottMcG · Nov 16, 2011 at 09:59 PM 1
Share

After doing a bunch of research on this and trying and failing, I've decided it's best to never insantiate GameObjects in the editor purely for the purpose of visualization. There's no reliable way to clean them up. $$anonymous$$y solution is a CustomEditor for my object and I represent the spawned prefabs with Handles. It means a car is represented by a cube in the editor, but there are no leaky GameObjects.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by damianf · Apr 13, 2012 at 05:20 AM

I'm experimenting with this right now. So far, I have an "okay" solution. (Scott, probably too late for you, but maybe not for the next poor sap?)

Basically, I do this:

 public void OnEnable()
 {
     m_MyGameObject = ...;
 }

 public void OnDisable()
 {
     if (m_MyGameObject != null)
     {
         Object.DestroyImmediate(m_MyGameObject);
         m_MyGameObject = null;
     }
 }

You will see that there are holes here...like, saving while the object is selected and then crashing, or something like that. If you can enforce that m_MyGameObject is parented to something that should never start the game with children, you can detect that in an Awake() call in a script on that object, warn, and delete the objects to be safe.

It's not remotely what I would call elegant, but it may be better than using handles. Also I've only just started doing this, so YMMV!

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Editor OnSceneGUI - Capture click on Gizmo 0 Answers

Editor.OnSceneGUI never called. 3 Answers

Custom Editor: How to get a reference to an existing object in the scene at the mouse position after drag-and-dropping a prefab into the scene? 1 Answer

If button highlighted 2 Answers

Display collider in scene view without object selected 6 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