Select objects close together progressively (+ and -)

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:

5081753--500003--TestRotation.jpg

I’ve build a Array for N gameObject.

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):

if (Input.GetKeyUp(KeyCode.W)) // here change the key in S
        {
            foreach (GameObject w in cylinder)
            {
                if (w.gameObject == Selection.activeObject)
                {
                    w.transform.Rotate(0, 0, -36); //here change the value in 36 for positive ecc..
                }
            }
        }

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…

if (Input.GetKeyDown(KeyCode.A))
        {
            for (int i = 0; i < cylinder.Length; i++)
            {
                if (cylinder[i].gameObject != Selection.activeObject)
                {
                    Selection.activeGameObject = cylinder[i].gameObject;
                    return;
                }
            }
         }

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! :slight_smile:

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.

int selectedCylinder = 0;
void Update() {
...
if (Input.GetKeyDown(KeyCode.A)) selectedCylinder--;
if (Input.GetKeyDown(KeyCode.D)) selectedCylinder++;
//loop around from left to right and vice versa
selectedCylinder = (selectedCylinder + cylinder.Length) % cylinder.Length;
...
cylinder[selectedCylinder].transform.Rotate(0,0,-36);
}
1 Like

Thanks for the advice about the editor and tip StarManta!!!. I’ll try it soon and let you know

Thanks Again!

Ok, the tip of StarManta work very well :slight_smile:

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…

public class HandRotateCylinder : MonoBehaviour
{

    public GameObject[] cylinder;
    int selectedCylinder = 1;

    bool disableKeyA = false;
    bool disableKeyD = false;

    private void Update()
    {

        if (Input.GetKeyUp(KeyCode.W))
        {
            cylinder[selectedCylinder].transform.Rotate(0, 0, -36);
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            cylinder[selectedCylinder].transform.Rotate(0, 0, 36);
        }

        selectedCylinder = (selectedCylinder + cylinder.Length) % cylinder.Length;
       
        if (Input.GetKeyDown(KeyCode.A) && disableKeyA == false)
        {
            disableKeyD = false;

            var lastItem = (cylinder.Length -1);

            selectedCylinder++;

            if (selectedCylinder == lastItem)
            {
                disableKeyA = true;
            }
        }


        if (Input.GetKeyDown(KeyCode.D) && disableKeyD == false)
        {
            disableKeyA = false;

            var firstItem = cylinder.GetLowerBound(0);

            selectedCylinder--;

            if (selectedCylinder == firstItem)
            {
                disableKeyD = true;
            }

        }
    }
}

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!