Hi, im trying to have it so that the script finds the current selected object, and then finds the index of that in an already made list. This is what I’m trying:
Transform currentSelection = Selection.activeTransform;
int currentSelectionIndex = myList.IndexOf (currentSelection);
The hope is that I would be able to affect the currently selected object with myList[currentSelectionIndex], but I can’t get it to work.
Edit:
The problem I’m getting is that everything I select (that’s in the list) is saying the index is -1.
List returns index to be -1 if it is not able to find that object in the given list. So your current selection object is not returning the data that is contained in list. It could be naming issue or anything. So please check that.
A sample script to show you that
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class abc : MonoBehaviour {
List<int> iList = new List<int>();
// Use this for initialization
void Start () {
iList.Add(2);
iList.Add(3);
iList.Add(5);
iList.Add(7);
}
// Update is called once per frame
public void ButtonClicked ()
{
int currentSelectionIndex = iList.IndexOf(7);
print (currentSelectionIndex);// It will return index 3
currentSelectionIndex = iList.IndexOf(9);
print (currentSelectionIndex);// it will give -1 as 9 is not present in the list.
}
}