How can i find Next and Previous Child ?

Hi guys,

Premise: I do not want to use an array to obtain a management easier and faster.

I expose my problem:

by Ray-Cast identify a GameObject (Current-Child - See photo)

Unity has in memory all the GameObject, How can I know the next child and previous child ?

12035-inspector.jpg

I don’t know of any API method to achieve this, but it can be done:

  1. Get the parent
  2. Walk through it’s children to find the index of current
  3. Get the index below or above and you’re done

C# Example to get the next child (or null if it doesn’t exist)

Transform GetNext()
{
   var myself=transform;
   var parent=transform.parent;
   var childCount=parent.childCount;
   for (int i=0;i<childCount-1;i++) { // skip the last, as it doesn't have a successor
     if (parent.GetChild(i)==myself)
        return parent.GetChild(i+1);
   }
   return null;
}

Code is untested from head, but should work.