Search Unity

Finding and deleting objects

Discussion in 'Scripting' started by HrC123, Sep 6, 2011.

  1. HrC123

    HrC123

    Joined:
    Feb 2, 2011
    Posts:
    140
    Hello
    I'm making mapeditor for my game, so first thing you have to assign how big ground plane you want to have on your map,

    in this example I have made ground with 1x1 size:

    $nest1.png

    Now I want to change size of ground to 5x6 so I get this:

    $nest2.png

    but the problem is what this old block still exists:

    $nest3.png

    so I have to make deleting objects tagged with "groundchunk" before the new chunks are made.
    How to make code that will do something like:
    Pick all objects in scene tagged with "groundchunk" and destroy them

    this "picking" is the problem, I have never used something similar before so I really don't know how to do it





    this is the code for "OK" button on window where you enter new values of ground size:

    Code (csharp):
    1.  
    2. if (GUI.Button(new Rect(Screen.width/2 +5, 200, 90, 20), "Ok"))
    3. {
    4.     SizeX = SizeXint;   [COLOR="seagreen"]//sets number of chunks on x-direction[/COLOR]
    5.     SizeZ = SizeZint;   [COLOR="seagreen"]//sets number of chunks on z-direction[/COLOR]
    6.     MapSize = false;   [COLOR="seagreen"]//closes window[/COLOR]
    7.     Groundbuild();       [COLOR="seagreen"]//calls part of code for creating recently assigned sizes [/COLOR]
    8. }
    9.  
    and this is "Groundbuild();" part which creates and align new set of ground chunks

    Code (csharp):
    1.  
    2. void Groundbuild ()
    3. {
    4.     for (int i = Mathf.FloorToInt(SizeX * SizeZ); i>0; i--)
    5.     {
    6.         float xi = -1*(SizeX-1)*3/2 + (i - Mathf.FloorToInt((i-1)/SizeX)*SizeX -1) * 3;
    7.         float zi = (SizeZ-1) * 3/2 - 3 * Mathf.FloorToInt((i-1)/SizeX);
    8.         Instantiate ( Groundchunk, new Vector3(xi, 0, zi), Quaternion.identity);
    9.     }
    10. }
    11.  
     
  2. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    Code (csharp):
    1. void ClearGroundTiles()
    2. {
    3.     GameObject[] groundTiles = GameObject.FindGameObjectsWithTag("groundchunk");
    4.     for (int i = 0; i < groundTiles.Length; i++)
    5.     {
    6.         Destroy(groundTiles[i]);
    7.     }
    8. }
     
  3. HrC123

    HrC123

    Joined:
    Feb 2, 2011
    Posts:
    140
    ty it's working :)