As the title says I want to take a selected GameObject and match it with the same GameObject thats in an Array of different GameObjects.
Would I use a for loop to do that? How would I set that up?
Thanks in advance.
Thanks @GroZZleR I couldn’t get that to work,( Im using UnityScript) but I found this
ArrayUtility.IndexOf(PreFabs, hero);
It seems to be about the same thing but isn’t returning the right index. It is just stuck at -1;
-1 means that the object is not found in the array, which might not be all that strange if the instance in the array is not the same instance as the one you provide in the method. Can you post some code?
var SelectedHeros : Transform;
var PreFabs : Transform[];
var index : int;
function Update()
{
index = ArrayUtility.IndexOf(PreFabs, SelectedHeros);
}
Ok, so I select my different Heros which are the same prefabs as in my PreFabs Array. I assume it is comparing them?
These are PreFabs that have not been added to the scene. Could I reference them from Resources?
Don’t know how it works with UnityScript, but in c# both the selectedHero and the instance in the preFabs Array will never be the same object as they are each a seperate instance of the same object.
Normally in c# you would override IEQuatable : c# - Comparing two instances of a class - Stack Overflow No idea how you would go about it in UnityScript though, not even sure if you can use it in Unity C# either.
Success, I was able to get it to work with a for loop. And tagging each one of my Prefabs a unique name and comparing those…I don’t know if this is the best way to do it but it works perfectly.
function Update()
{
for (var i = 0; i < PreFabs.Length; i++)
{
if (PreFabs[i].tag == SelectedHero.tag)
{
index = i;
break;
}
}