How to loop through 3d gameobjects at Fixed Position?

I want to loop through 3d game objects at Fixed Position using a UI botton but I don’t know where t start and how to do so?
Hence can anyone give me some advice or help me to do so?

Hey !
First some questions, are you creating objects while the game is playing or are they already here ?

If they are already here, you cando something like this :

using UnityEngine;

public class SelectObject : MonoBehaviour
{
    [SerializeField] private GameObject[] myArrayOfGameObject;

    private int _currentArrayIndex = -1;
    private GameObject _gameObjectSelected;
  
    public void SelectNextObjectInArray()
    {
        if (_currentArrayIndex == myArrayOfGameObject.Length - 1)
        {
            _currentArrayIndex = 0;
        }
        else
        {
            _currentArrayIndex++;
        }
        
        _gameObjectSelected = myArrayOfGameObject[_currentArrayIndex];
        Debug.Log("current Object: "+ _gameObjectSelected);
    }
}

To explain what happen :

  • In the function SelectNextObjectInArray(), you will use an array that contain the reference of all the concerned objects (that you set in the inspector).
  • Each time the button is clicked you will set a variable, here it is _currentArrayIndex, (type Int) with the next value. You must know that the value is set at default to -1 so you get the first item and not the second one.
  • The if statement is used to check if you are at the end of your array and if yes, to reset the index to 0.
  • After getting the next index for your array, you can set a variable, here it is _gameObjectSelected, (type GameObject) with the gameObject store in the array using the new index.

I hope I was understable, of course I’m pretty sure they are other ways to do it, possibly better one too.
If you have any questions, don’t hesitate to ask.

Cheers

hi!
firstly the gameobjects are as prefabs in project.
secondly I want the gameobject on specific position like I am making an ar ring try on using a manoimotion SDK
so in this I can only add one finger according to it script and I wnt to add multiple prefabs to it!!
ummi know it will be more to ask but can u point out in the code where should I change like I am new to soding and somewhat learning … so if u could possible demonstrate it more in my codes.

using UnityEngine;
using TMPro;
using UnityEngine.Android;

/// <summary>
/// This will place out a ring on the ring finger using the information from the FingerInfoGizmo
/// </summary>
public class RingExample : MonoBehaviour
{
    /// <summary>
    /// The finger gizmo contains the finger information.
    /// </summary>
    private FingerInfoGizmo fingerInfoGizmo;

    /// <summary>
    /// Ring parts for only display half of the ring at a time.
    /// </summary>
    public GameObject[] ringPrefabParts;

    /// <summary>
    /// Used to set wich part of the ring prefabs to show.
    /// </summary>
    private bool isFront = true;

    /// <summary>
    /// Gesture inforamtion.
    /// </summary>
    private GestureInfo gestureInfo;

    /// <summary>
    /// Palmside.
    /// </summary>
    private HandSide palm = HandSide.Palmside;

    /// <summary>
    /// The gameobject contaning the image of the outlined hand. 
    /// </summary>
    [SerializeField]
    private GameObject outlineImage;

    /// <summary>
    /// The UI text for the current finger to be shown.
    /// </summary>
    public TMP_Text changeFingerText;

    /// <summary>
    /// The string that will be used on the toggle finger button.
    /// </summary>
    private string buttonText = "Toggle finger for ring. Current finger: ";

    private void Start()
    {
        ///Sets the screen orienation to portrait mode.
        Screen.orientation = ScreenOrientation.Portrait;

        if (fingerInfoGizmo == null)
        {
            try
            {
                fingerInfoGizmo = GameObject.Find("TryOnManager").GetComponent<FingerInfoGizmo>();
            }
            catch 
            {
                Debug.Log("Cant find 'TryOnManager' GameObject");
            }
        }

        SetManoMotionSettings();
        SetSelectFingerButtonText();
        GameObject.Find("Finger").SetActive(false);
    }

    private void SetManoMotionSettings()
    {
        ManomotionManager.Instance.ShouldRunFingerInfo(true);
        ManomotionManager.Instance.ShouldCalculateGestures(true);
        int ringFingerIndexDefault = 4;
        ManomotionManager.Instance.ToggleFingerInfoFinger(ringFingerIndexDefault);
    }

    void Update()
    {
        ///Updates the gestureinfo
        gestureInfo = ManomotionManager.Instance.Hand_infos[0].hand_info.gesture_info;

        ///While open hand gesture is performed the ring should show.
        if (gestureInfo.mano_class == ManoClass.GRAB_GESTURE)
        {
            fingerInfoGizmo.ShowFingerInformation();
            ShowRing();
        }

        ///if not open hand gesture is performed the ring dont show.
        else
        {
            DontShowRing();
        }
    }

    private void ShowRing()
    {
        float centerPosition = 0.5f;

        ///Gets the position between the 2 finger points from the finger gizmo.
        Vector3 ringPlacement = Vector3.Lerp(fingerInfoGizmo.LeftFingerPoint3DPosition, fingerInfoGizmo.RightFingerPoint3DPosition, centerPosition);

        ///Place the ring at the ring placement position.
        transform.position = ringPlacement;

        ///Sets the rotation of the ring in relation the the finger position when hand is rotated 
        transform.LookAt(fingerInfoGizmo.LeftFingerPoint3DPosition);

        ///Scale the ring with the width from the 2 finger points and multiplyed by a scaleModifier.
        transform.localScale = new Vector3(fingerInfoGizmo.WidthBetweenFingerPoints, fingerInfoGizmo.WidthBetweenFingerPoints, fingerInfoGizmo.WidthBetweenFingerPoints);

        ///When Palm is showing the scale gets inverted to show the back of the ring.
        if (gestureInfo.hand_side == palm)
        {
            ActivateRingParts(!isFront);
            transform.localScale = new Vector3(-transform.localScale.x, -transform.localScale.y, -transform.localScale.z);
        }
        else
        {
            ActivateRingParts(isFront);
        }

        ///Disables the outline image.
        outlineImage.SetActive(false);
    }

    /// <summary>
    /// Activate or deactive parts of the ring depengind on wich half should show.
    /// </summary>
    /// <param name="front">bool to set the values.</param>
    private void ActivateRingParts(bool front)
    {
        ringPrefabParts[0].SetActive(front);
        ringPrefabParts[1].SetActive(front);
        ringPrefabParts[2].SetActive(!front);
    }

    /// <summary>
    /// Enabled the outline image and move the ring to -Vector3.one so its not visable.
    /// </summary>
    private void DontShowRing()
    {
        outlineImage.SetActive(true);
        transform.position = -Vector3.one;
    }

    /// current finger index, 4 default for ring finger;
    private int currentFingerIndex = 4;

    /// <summary>
    /// Toggles the finger that should use the ring and also updated the UI text.
    /// </summary>
    
    
    public void ToggleFingerForRing()
    {

        if (currentFingerIndex < 5)
        {
            currentFingerIndex++;
        }
        else
        {
            currentFingerIndex = 0;
        }

        ManomotionManager.Instance.ToggleFingerInfoFinger(currentFingerIndex);
        SetSelectFingerButtonText();
    }


    /// <summary>
    /// Sets the button text on the UI to match current finger info.
    /// </summary>
    private void SetSelectFingerButtonText()
    {
        switch (currentFingerIndex)
        {
            case 0:
                changeFingerText.text = buttonText + "None";
                break;
            case 1:
                changeFingerText.text = buttonText + "Thumb";
                break;
            case 2:
                changeFingerText.text = buttonText + "Index";
                break;
            case 3:
                changeFingerText.text = buttonText + "Middle";
                break;
            case 4:
                changeFingerText.text = buttonText + "Ring";
                break;
            case 5:
                changeFingerText.text = buttonText + "Pinky";
                break;
            default:
                break;
        }
    }
    
}

fixed position means it will (rings prefabs) appear on my index finger which a fixed index for my prefabs…