Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by pdunton · Feb 26, 2014 at 04:42 AM · javascriptarraysindex

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);
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by Eric5h5 · Feb 26, 2014 at 05:14 AM

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pdunton · Feb 26, 2014 at 05:31 AM 0
Share

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

avatar image pdunton · Feb 26, 2014 at 06:41 AM 0
Share

Never$$anonymous$$d, I figured it out. Thanks!

avatar image dinesh-jadhav pdunton · Jun 22, 2017 at 08:55 AM 0
Share

How did you find out an index of a GameObject in Array of GameObject.....?

avatar image
6

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoZain · Aug 19, 2016 at 09:02 PM 0
Share

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

avatar image marx13maxz · Jun 19, 2018 at 02:14 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is this considered bad coding 1 Answer

Instantiate multiple objects from an array 1 Answer

My array does not update when object is destroyed. How do I fix it? (java) 2 Answers

For values in array 2 Answers

Array Not Appering In The Inspector 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges