UnityScript.contains method

Hi everyone, I was wondering is there a method similar to the .contains method of C#? I keep trying to use .contains in my script and I get this error Contains is not a member of string.

public var ExampleArray:String[];

function Start(){
ExampleArray.Add("Element1");
//Contains is not a member of string
if(ExampleArray.Contains("Element0"){
Debug.Log("ExampleArray contains Element0")
}

Use a LIST instead of an array. You need to use System.Collections.Generic to use a list. Try just replacing your code with this.

@script ExecuteInEditMode()

import System.Collections.Generic;

public var ExampleList : List.<String> = new List.<String>();
public var cap : int = 1;

function Start()
{
    ExampleList.Capacity = cap;
    
    if(ExampleList.Count < cap)
    {
        ExampleList.Add("Element0");
    }

    if(ExampleList.Contains("Element0"))
        Debug.Log("ExampleList contains Element0");
}

Contains is a member of System.Array, it’s not a C# method. Therefore it works fine with JS as-is. The problem is that your script had other errors.

public var ExampleArray : String[];

function Start() {
	if (ExampleArray.Contains("Element0")) {
		Debug.Log("ExampleArray contains Element0");
	}
}

The parentheses and braces need to match up correctly. Also Add is not a member of System.Array. Arrays are fixed-size and have no methods for adding or removing elements.

Addition to clunk47’s answer. If you need native arrays, you can always go back with function ToArray() : T[]:

var list : List.<String> = new List(["foo", "bar", "baz"]);
var native : String[] = list.ToArray();

Alternatively, you can use static version of System.Array.IndexOf(array : T[], item : T) : boolean:

if (System.Array.IndexOf(array, item) != -1) {
   // found...
}