- Home /
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);
Answer by Eric5h5 · Feb 26, 2014 at 05:14 AM
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.
How did you find out an index of a GameObject in Array of GameObject.....?
Answer by nesis · Feb 26, 2014 at 05:21 AM
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[i] for some int i, you can use arrayOfPairs[i].myGameObject and arrayOfPairs[i].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;
Your answer
Follow this Question
Related Questions
How to set parents of all objects in an array? 1 Answer
Unity c# indexoutofrangeexception: array index is out of range 1 Answer
Type function(): Object[] does not support Slicing 1 Answer
Why can't I push and pop from string array? 1 Answer
Change rigidbody values on mutliple objects (ARRAY) 1 Answer