Search Unity

Select objects close together progressively (+ and -)

Discussion in 'Scripting' started by MedalHellWay, Oct 18, 2019.

  1. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Here again for an easy question ...or not?

    I find myself creating a new test with rotation and object selection. Here the image of test:

    TestRotation.jpg

    I've build a Array for N gameObject.

    Code (CSharp):
    1. public GameObject[] cylinder;
    I move the rotation of gameobject with keys W and S. It's very simple and I managed to create it this way (for positive and negative rotation):

    Code (CSharp):
    1.  
    2. if (Input.GetKeyUp(KeyCode.W)) // here change the key in S
    3.         {
    4.             foreach (GameObject w in cylinder)
    5.             {
    6.                 if (w.gameObject == Selection.activeObject)
    7.                 {
    8.                     w.transform.Rotate(0, 0, -36); //here change the value in 36 for positive ecc..
    9.                 }
    10.             }
    11.         }
    Now with the keys A and S I would like select objects (right or left) for the entire length of the array. Reading the Unity help I found it useful to use "Selection.activeGameObject (using UnityEditor)" but I can't read the entire length of the array. If I use...

    Code (CSharp):
    1.  
    2. if (Input.GetKeyDown(KeyCode.A))
    3.         {
    4.             for (int i = 0; i < cylinder.Length; i++)
    5.             {
    6.                 if (cylinder[i].gameObject != Selection.activeObject)
    7.                 {
    8.                     Selection.activeGameObject = cylinder[i].gameObject;
    9.                     return;
    10.                 }
    11.             }
    12.          }
    13.  
    I know how I can select the second item and then return to the first one (obviously). Basically I wrapped myself in this little problem ... How to proceed? Ideas? Tips?

    Thanks for any help! :)
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If this is for in-game code, you shouldn't use UnityEditor classes. They won't even compile when you go to build your standalone game.

    It looks like you're just trying to move to the next/previous cylinder, you should just have an int representing the current index of the selected cylinder.
    Code (csharp):
    1.  
    2. int selectedCylinder = 0;
    3. void Update() {
    4. ...
    5. if (Input.GetKeyDown(KeyCode.A)) selectedCylinder--;
    6. if (Input.GetKeyDown(KeyCode.D)) selectedCylinder++;
    7. //loop around from left to right and vice versa
    8. selectedCylinder = (selectedCylinder + cylinder.Length) % cylinder.Length;
    9. ...
    10. cylinder[selectedCylinder].transform.Rotate(0,0,-36);
    11. }
     
    MedalHellWay likes this.
  3. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Thanks for the advice about the editor and tip StarManta!!!. I'll try it soon and let you know

    Thanks Again!
     
  4. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Ok, the tip of StarManta work very well :)

    I have implemented the script in such a way that the selection of the object of the first element and the last of the array stops. Below the total script...

    Code (CSharp):
    1. public class HandRotateCylinder : MonoBehaviour
    2. {
    3.  
    4.     public GameObject[] cylinder;
    5.     int selectedCylinder = 1;
    6.  
    7.     bool disableKeyA = false;
    8.     bool disableKeyD = false;
    9.  
    10.     private void Update()
    11.     {
    12.  
    13.         if (Input.GetKeyUp(KeyCode.W))
    14.         {
    15.             cylinder[selectedCylinder].transform.Rotate(0, 0, -36);
    16.         }
    17.  
    18.         if (Input.GetKeyDown(KeyCode.S))
    19.         {
    20.             cylinder[selectedCylinder].transform.Rotate(0, 0, 36);
    21.         }
    22.  
    23.         selectedCylinder = (selectedCylinder + cylinder.Length) % cylinder.Length;
    24.        
    25.         if (Input.GetKeyDown(KeyCode.A) && disableKeyA == false)
    26.         {
    27.             disableKeyD = false;
    28.  
    29.             var lastItem = (cylinder.Length -1);
    30.  
    31.             selectedCylinder++;
    32.  
    33.             if (selectedCylinder == lastItem)
    34.             {
    35.                 disableKeyA = true;
    36.             }
    37.         }
    38.  
    39.  
    40.         if (Input.GetKeyDown(KeyCode.D) && disableKeyD == false)
    41.         {
    42.             disableKeyA = false;
    43.  
    44.             var firstItem = cylinder.GetLowerBound(0);
    45.  
    46.             selectedCylinder--;
    47.  
    48.             if (selectedCylinder == firstItem)
    49.             {
    50.                 disableKeyD = true;
    51.             }
    52.  
    53.         }
    54.     }
    55. }
    One thing doesn't happen. If the value of the array is zero or the last and the relative A & D keys are pressed, they are not disabled, but only subsequently. I created the condition but it is read later, why does this happen? (trivial question for you but not for me since I am a noob ...)

    P.S: Now I only have to highlight the selected objects and I have also finished this test

    Thanks!