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 /
  • Help Room /
avatar image
0
Question by Crash0 · Nov 27, 2016 at 03:04 PM · c#gameobjectinstantiatedestroy

How to destroy on exiting PlayMode/EditMode?

Hi, Im new to the Unity framework, and having some issues.

Im instantiating an array of GameObjects from code in the Start().

 Tiles = new GameObject[SizeX, SizeZ];
 for (int x = 0; x < SizeX; x++)
 {
     for (int z = 0; z < SizeZ; z++)
     {
         var tile = (GameObject) Instantiate(TileTemplate, new Vector3(x, 0, z), Quaternion.identity);
         tile.GetComponentInChildren<Renderer>().enabled = false;
         Tiles[x, z] = tile;
     }
 }

Where TileTemplate is a public GameObject. The GameObjects are instantiating as they should, and Scripts are running fine.

The problem occurs when I´m exiting PlayMode. The GameObjects are persisting them selvs in the editor, and are duplicated the next time I start PlayMode.

One thing is that I dont totally understand why (Editor state-full?), but ok.

So I´m then looking for something like Stop() or or onApplicationStop() which do not exist, but find my way to

 EditorApplication.playmodeStateChanged += PlaymodeStateChanged;

and

 private void PlaymodeStateChanged()
 {
     if (!EditorApplication.isPlayingOrWillChangePlaymode &&
             EditorApplication.isPlaying )
     {
         foreach (var tile in Tiles)      
         {
             DestroyImmediate(tile);
             //Or
             Destroy(tile);
         }
         Tiles = null;
     }
 }

And its not working... the method is called and the foreach loop is running, but its not destroying the GameObjects.

So I wonder is my code faulty, or is there something fundamental of the Unity framework I got wrong?

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
1
Best Answer

Answer by Adam-Mechtley · Nov 28, 2016 at 09:31 AM

Hi! If something is instantiated in that way inside a MonoBehaviour's Start () function, then it should automatically get cleaned up when the editor stops playing. You don't have something like ExecuteInEditMode on your script, do you?

Comment
Add comment · Show 4 · 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 Crash0 · Nov 29, 2016 at 09:57 AM 0
Share

Yeah, that was the problem (y). But Im not quite getting why it happens?

I am doing a null check on the array before instantiating. So if the editor creates the objects, they should be stored in the array. Im expecting that the editor would destroy the class and the GameObjects when starting play-mode. Not dereferencing them and keeping them in the scene.

avatar image Adam-Mechtley Crash0 · Nov 29, 2016 at 12:15 PM 0
Share

If they're instantiated in the editor when the application is not playing, then they will persist in the editor if they are not destroyed.

The problem here is that multidimensional arrays (i.e. your Tiles property) cannot be serialized, and so the data stored in it will not persist between domain reloads (i.e. playing/stopping editor) even though ExecuteInEdit$$anonymous$$ode is on. What you need to do is something like this:

 [System.Serializable]
 public class TileRow {
     [SerializeField]
     private List<Tile> m_Row = new List<Tile> ();
     
     public TileColumn (Tile[] tiles) {
         m_Row.AddRange (tiles);
     }
     public int Count { get { return m_Row.Count; } }
     public Tile this[int index] {
         get { return m_Tiles[index]; }
         set { m_Tiles[index] = value; }
     }
     public void Add (Tile tile) { m_Tiles.Add (tile); }
     public void RemoveAt (int index) { m_Tiles.RemoveAt (index); }
 }

 public class $$anonymous$$yClass : $$anonymous$$onoBehaviour {
     [SerializeField]
     private int m_SizeX = 0;
     [SerializeField]
     private int m_SizeZ = 0;
     [SerializeField]
     private List<TileRow> m_Tiles = new List<TileRow> ();

     protected virtual void OnValidate () {
         SynchronizeTiles ();
     }

     protected virtual void Start () {
         SynchronizeTiles ();
     }
     
     private void SynchronizeTiles () {
         while (m_Tiles.Count < m_SizeZ)
             m_Tiles.Add (new TileRow (new Tile[m_SizeX]));
         while (m_Tiles.Count > m_SizeZ) {
             foreach (var tile in m_Tiles[m_Tiles.Count - 1]) {
                 if (tile != null)
                     DestroyImmediate (tile);
             }
             m_Tiles.RemoveAt (m_Tiles.Count - 1);
         }
         for (int x = 0; z < m_SizeZ; ++z) {
             while (m_Tiles[z].Count < m_SizeX)
                 m_Tiles[z].Add (null);
             while (m_Tiles[z].Count > m_SizeX) {
                 int i = m_Tiles[z].Count - 1;
                 if (m_Tiles[z][i] != null)
                     DestroyImmediate (m_Tiles[z][i]);
                 m_Tiles[z].RemoveAt (i);
             }
             for (int z = 0; x < m_SizeX; ++x) {
                 if (m_TilePrefab == null) {
                     if (m_Tiles[z][x] != null)
                         DestroyImmediate (m_Tiles[z][x]);
                     m_Tiles[z][x] = null;
                 }
                 else {
                     if (m_Tiles[z][x] == null)
                         m_Tiles[z][x] = (Tile)Instantiate (m_TilePrefab, new Vector3 (x, 0f, z), Quaternion.identity);
                 }
                 if (m_Tiles[z][x] != null)
                     m_Tiles[z][x].GetComponentInChildren<Renderer>().enabled = false;
             }
         }
  }

}

avatar image Crash0 Adam-Mechtley · Nov 29, 2016 at 02:24 PM 0
Share

O$$anonymous$$, TIL: Unity relays heavily on serialization.

But do I have to declare types explicit as serializable ? I thought that was implicit when a class has the System.Serializable attribute.

Btw, is there a Wiki / reffrence page on Unity Internals ?

Show more comments

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

276 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate and then destroy a prefab in a list. C# 1 Answer

What is Casting in Unity ? 1 Answer

RPG instantiate problem 3 Answers

Game Object doesn't instantiate at Mouse Position 3 Answers

The bullet never destroyed!!! 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