Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question how do i get the index of an array?

Discussion in 'Scripting' started by unity_005946E12C5F7D78583B, Jul 7, 2023.

  1. unity_005946E12C5F7D78583B

    unity_005946E12C5F7D78583B

    Joined:
    Dec 19, 2022
    Posts:
    35
    i want to make a system that identifies the array index of the object that the player touched, i have searched a lot for this and the closest thing i managed to finf was IndexOf or something of the System.Array.IndexOf, but the code doesnt allow me to use that.

    is there a way to do this? whats the System. i need to use? or do i just use a list instead of an array?
     
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    well

    Code (CSharp):
    1. for(int i = 0; i< array.length;i++){
    2. if(array[i]==objectTouched){index = i;break;}
    3. }
    its not clean, ideally you should structure your data so that the object contains info on its own index to avoid iterating through the whole array
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Please ask scripting questions on the scripting forum. Your post has nothing to do with 2D.

    I'll move your post for you.

    Thanks.
     
    Bunny83 likes this.
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,342
    In a similar vein as karderos, why dont use simply use colliders to detect when a player touches an object? Thats what they are for. I guess, why do you need the index of the array when you can probably just create a function that says if i touch this object (or any of the multiple objects) then tell the ObjectController to do _____ ?
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    The others implied so already, but why do you think you need this index? Depending on your architecture this may be a valid usecase, but it sounds more like a XY Problem. If you tell us the actual usecase we may be able to offer more specific solutions.
     
    PraetorBlue likes this.
  6. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,913
    IndexOf
    works, you probably didn't use it right. It takes some experience with normal computer programming in order to read the description and understand what it's telling you to do.

    A different Unity trick I use is when the objects that can be touched also have scripts on them. Just make an extra variable and write the index on each object's script. You don't need that, since IndexOf can find it anyway, but it makes things easier and doesn't waste much space.
     
    arkano22 and Bunny83 like this.
  7. unity_005946E12C5F7D78583B

    unity_005946E12C5F7D78583B

    Joined:
    Dec 19, 2022
    Posts:
    35
    well, i already found a solution with lists but thanks anyway!