- Home /
Alternative for array.contains() method
I was using this method in a while ago, but now Unity doesn't have it in their MonoBehavior. Is there an alternative to this method?
Answer by Eric5h5 · Dec 18, 2014 at 07:07 PM
Array.Contains has nothing to do with MonoBehaviour. The JS Array class never had Contains (and JS Arrays should never be used anyway). System.Array doesn't have Contains unless you use the extension method (requires importing the Linq namespace). Otherwise use Array.IndexOf. Or List.Contains.
Array.IndexOf is not in the list of array methods. :( I don't get it. Why did Unity get rid of all methods of finding a certain element in an array? .Find, .Exist and .IndexOf aren't in there. What's up with this?
They didn't get rid of anything. I don't think you actually read what I wrote, but regarding IndexOf: http://msdn.microsoft.com/en-us/library/system.array.indexof(v=vs.110).aspx
Ok, but I don't need it to return the index of the element that it found. I need it see if the content inside of the array exists. I used to type in "if (validtags.contains(other.gameobject.tag))" and it worked, no namespaces imported. Then an unity update came and the console kept crying ".contains is not part of the array methods". :P
Ok, I switched from arrays to lists, and so far no complaints. Thanks for the help
$$anonymous$$ight help if you read the docs for IndexOf..."The zero-based index of the first occurrence of value in the entire array, if found; otherwise, –1." Array.contains never existed, so you couldn't have used that. If you mean Array.Contains, that needs Linq as I mentioned since it's an extension method. No Unity updates have ever changed this.
Your answer
Follow this Question
Related Questions
Array.map Support 1 Answer
2D Array and other Script method access 1 Answer
Editing a jagged array? 1 Answer