How to find the index of an Object in an Array

I have two arrays, one to hold gameObjects and the other to hold Vector3s. In a for loop, I am using Vector3.Lerp() to move each currentObject to its corresponding position. So if the for loop reaches objects[3] I want to acsess the index of that object to match it with its Vector3. So basically (not correct syntax, I know)

currentObject.transform.position = Vector3.Lerp(currentObject.transform.position, /*This is what i need help with, here is a guess*/ vector3array[currentObject.index], Time.deltaTime);

2 Answers

2

You can use System.Array.IndexOf, namely System.Array.IndexOf (vector3array, currentObject).

How would i fit this into the rest of my script? I'm a little confused.

Nevermind, I figured it out. Thanks!

If these two arrays are always going to be paired one-to-one, I’d just make a small class to contain the objects you want, then make a single array of that class. As an example in C#:

using UnityEngine;
using System.Collections;

//Step 1: Define a class that contains the types you want. 
//        (put it anywhere, I usually dump it in the script 
//        I'm using the class in, or in a file of its own 
//        if it's used in multiple places)
//Step 2: Add the attribute System.Serializable to that class.
//Step 3: In a script somewhere, declare an array of your type.

//The below line tells Unity to show this class' 
//"Serializable" member variables in the Inspector, 
//if it happens to get used as a public variable 
//in a script somewhere... such as this one!
//(A "Serializable" member variable is any class
//or primitive type that Unity can show in the 
//inspector. Eg, classes include Rigidbody, 
//GameObject, Texture2D (anything inheriting from 
//Object) and primitive types include int, float, 
//string, and enum.
[System.Serializable]
public class MyPairs {
    //NOTE: this class does NOT inherit from MonoBehaviour like 
    //scripts do by default! This just holds pairs, so won't need 
    //any of MonoBehaviour's methods like Update(), Start(), etc
    
    //Here are the variables you want to store in 
    //each element of the array. You can add as many 
    //variables as you want here.
    public GameObject myGameObject;
    public Vector3 myVector3;
}

//Here's the actual class this script was made for (so the
//script should be called MyScript.cs)
public class MyScript : MonoBehaviour {

    //Since we've made the MyPairs class "Serializable",
    //this array is now able to be shown as a variable in 
    //the inspector, and able to have its values saved 
    //when you save in Unity.
    public MyPairs[] arrayOfPairs;
}

Now when you access arrayOfPairs for some int i, you can use arrayOfPairs_.myGameObject and arrayOfPairs*.myVector3 to get the GameObject and Vector3 you want paired together.*_

Thank you very much for explaining this so thoroughly! This helped me understand classes a lot better.

I am getting this Error NullReferenceException: Object reference not set to an instance of an object this is the line it is pointing. ButtonPairs[0].SelectButton = button.name;