c# - Get the position of a gameobject in an array -


i using script sort array alphanumeric:

public class alphanumcomparatorfast : icomparer<string> {     public int compare(string x, string y)     {         string s1 = x string;         if (s1 == null)         {             return 0;         }         string s2 = y string;         if (s2 == null)         {             return 0;         }          int len1 = s1.length;         int len2 = s2.length;         int marker1 = 0;         int marker2 = 0;          // walk through 2 strings 2 markers.         while (marker1 < len1 && marker2 < len2)         {             char ch1 = s1[marker1];             char ch2 = s2[marker2];              // buffers can build characters in each chunk.             char[] space1 = new char[len1];             int loc1 = 0;             char[] space2 = new char[len2];             int loc2 = 0;              // walk through following characters digits or             // characters in both strings starting @ appropriate marker.             // collect char arrays.                         {                 space1[loc1++] = ch1;                 marker1++;                  if (marker1 < len1)                 {                     ch1 = s1[marker1];                 }                 else                 {                     break;                 }             } while (char.isdigit(ch1) == char.isdigit(space1[0]));                          {                 space2[loc2++] = ch2;                 marker2++;                  if (marker2 < len2)                 {                     ch2 = s2[marker2];                 }                 else                 {                     break;                 }             } while (char.isdigit(ch2) == char.isdigit(space2[0]));              // if have collected numbers, compare them numerically.             // otherwise, if have strings, compare them alphabetically.             string str1 = new string(space1);             string str2 = new string(space2);              int result;              if (char.isdigit(space1[0]) && char.isdigit(space2[0]))             {                 int thisnumericchunk = int.parse(str1);                 int thatnumericchunk = int.parse(str2);                 result = thisnumericchunk.compareto(thatnumericchunk);             }             else             {                 result = str1.compareto(str2);             }              if (result != 0)             {                 return result;             }         }         return len1 - len2;     } } 

and script array attached cubes:

void awake() {     allcubes = gameobject.findgameobjectswithtag("cube");     allcubes = allcubes.orderby(obj => obj.name, new alphanumcomparatorfast()).toarray(); } 

this output in inspector:

https://i.gyazo.com/0dd2c9fdf8e07f9f971cd074695fc061.png

my question is: there way find out @ number current object in array is? added script cubes, there way if put code in script return position of gameobject in array list? example if debug cube5, return:

my position in array 4

my goal next cube of next cube of each cube :). cube1 should give me cube3, cube3 should give me cube5 etc.. thought use answer , variable + 2. want this, because each cube has information of next cube of next cube.

you loop on array , compare gameobject instance. if matches, return current loop index. if nothing matches, return negative number since array cannot have negative number.

public gameobject[] allcubes;  int findindex(gameobject target) {     (int = 0; < allcubes.length; i++)     {         //if find index return current index         if (allcubes[i] == target)         {             return i;         }     }     //nothing found. return negative number     return -1; } 

to use in scrip, pass gameobject variable current gameobject script attached to.

int index = findindex(gameobject); 

edit:

i suggest change current design little-bit , put code in question on empty gameobject alphanumcomparatorfast script executed once. can access array result other scrips. see this post how that. saying because speed game.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -