Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cant Destroy Object them do something...going mad here

Discussion in 'Scripting' started by Anim, Aug 25, 2009.

  1. Anim

    Anim

    Joined:
    Aug 11, 2008
    Posts:
    289
    Hi all

    Ive been at this for a day now. Racking my brain trying to get what I can see on paper working in Unity.

    Ok.....imagine this:

    I have a row of 10 tiles across the screen (0 to 9), if the user clicks a tile it destroys it and the other tiles fill the gap.

    Thats the logic. Trying to get it to work is something else.

    On the tile that is clicked there is a script called DestroyMe.js

    which contains the following:
    Code (csharp):
    1.  
    2. function OnMouseUp(){
    3.     Debug.Log("Sending Kill for " + gameObject.name);
    4.     MainSetup.deleteTile(gameObject);
    5. }

    That is calling MainSetup.deleteTile()

    Which has the following code in it:

    Code (csharp):
    1. static function deleteTile(name : GameObject){
    2. var i = 0;
    3. var n = 0;
    4. var targetPos;
    5. var targetPlaceHolder;
    6. var aName = "";
    7.  
    8.     Debug.Log("killing " + name.name);
    9.    
    10.      Destroy(name);
    11.  
    12.     // yield WaitForSeconds(1);
    13.      Debug.Log("continuing....");
    14.        
    15.         for (i=0 ; i <9; i++)
    16.         {
    17.             aName = "TileCopy" + i;
    18.             Debug.Log("aName = " + i);
    19.            
    20.             if(GameObject.Find(aName) == null)
    21.                 {  
    22.                 Debug.Log("Found Empty Space at " + i);
    23.                 targetPlaceHolder = "s" + i;
    24.                 targetPos = GameObject.Find(targetPlaceHolder).transform.position;
    25.                 Debug.Log("Target Position is" + targetPos);
    26.                
    27.                 for (n=i; n<=9;n++)
    28.                    {
    29.                      Debug.Log("Looking for next filled tile.." + n);
    30.                      aName = "TileCopy" + n;
    31.                      if(GameObject.Find(aName) != null)
    32.                         {
    33.                         Debug.Log("Found filled tile at " + n + ", everything ready to move...."); 
    34.                         Ani.Mate.To(GameObject.Find(aName).transform,0.1, {"position": targetPos, "direction": EasingType.InOut});
    35.                         }
    36.                    
    37.                     }
    38.                
    39.                 return;  // one tile should have moved by now.
    40.                 }
    41.    
    42.          }
    43. }
    Now, if i run this then the console outputs "Sending Kill for" and the tile you clicked on,

    What should happen is the tile gets deleted and the other tiles fill the gap.

    If I rem out
    Code (csharp):
    1. Destroy(name);
    Then the code runs and the console fills up with all the details from ALL the Debug.Logs but the clicked on tile hasnt had time to be destroyed so the actual code doesn't do what its supposed too

    So I added:
    Code (csharp):
    1. yield WaitForSeconds(1);
    Which doesn't work, it stops the entire script from running. Not even the Debug.Log("killing " + name.name); gets run.

    So, anybody help me here would be very very appreciated.

    Thanks
     
  2. Anim

    Anim

    Joined:
    Aug 11, 2008
    Posts:
    289
    This issue has been solved now. Rather than using the yield I moved the Destroy code to the bottom of the function and changed the tile name when its clicked on so it doesn't find it when scanning for holes. Thanks NCarter on IRC for this.