Search Unity

Drop All Selected To Floor

Discussion in 'Made With Unity' started by RyuMaster, Jun 16, 2011.

  1. RyuMaster

    RyuMaster

    Joined:
    Sep 13, 2010
    Posts:
    468
    Just a simple editor script, if someone will search for it. I was placing many tree sprites around terrain, I needed something to drop all selected objects to floor.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class DropAll : ScriptableObject
    6. {
    7.  
    8.     [MenuItem("Custom/DropAll")]
    9.     static void DropAllObjects()
    10.     {
    11.        
    12.         Transform[] all = Selection.transforms;
    13.         for (int i = 0; i < all.Length; i++)
    14.         {
    15.  
    16.             //Check the base first
    17.             if(all[i].collider != null)
    18.             CheckCollider(all[i]);
    19.  
    20.             //Then, check the children
    21.  
    22.             foreach (Transform child in all[i])
    23.             {
    24.                 CheckCollider(child);
    25.             }
    26.  
    27.         }
    28.  
    29.  
    30.     }
    31.  
    32.     static void CheckCollider(Transform input)
    33.     {
    34.  
    35.         RaycastHit[] hits;
    36.         hits = Physics.RaycastAll(new Vector3(input.position.x,input.position.y-input.gameObject.collider.bounds.size.y/2,input.position.z), -Vector3.up, 1000.0F);
    37.         int z = 0;
    38.         while (z < hits.Length)
    39.         {
    40.             RaycastHit hit = hits[z];
    41.             z++;
    42.             input.Translate(new Vector3 (0, 0 ,hit.distance));
    43.         }
    44.  
    45.     }
    46.  
    47. }
     
  2. SpaceRay

    SpaceRay

    Joined:
    Feb 26, 2014
    Posts:
    455
    Hello, Sorry that I have very little knowdlege and experience in coding, so I have copied this to a new csharp script, and tried to use this but I get this 3 errors

    1
    Assets/DropAll.cs(8,10): error CS0246: The type or namespace name `MenuItem' could not be found. Are you missing a using directive or an assembly reference?
    2
    Assets/DropAll.cs(8,10): error CS0246: The type or namespace name `MenuItemAttribute' could not be found. Are you missing a using directive or an assembly reference?
    3
    Assets/DropAll.cs(12,31): error CS0103: The name `Selection' does not exist in the current context

    Please, does anyone can explain why is this happening and how to fix this?

    Thanks very much for any possible help

    Maybe the reason is that this from 2011 and we are in 2015 and the csharp inside unity could have changed.

    -------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------

    UPDATE to the above

    I have just found now this inside the Unity Asset Store that seems to be something in some similar way

    Project and Drop to Floor

    https://www.assetstore.unity3d.com/en/#!/content/26645
     
    Last edited: Jan 30, 2015