.Contains(array)
hi, i'm trying to do this
string[] Enemys = new string[2] {"Cube","Enemy"};
if (hit.transform.name.Contains(Enemys)){ Debug.Log( "Enemy is clicked by mouse"); }
but it's not working and i'm stuck and not sure why this isn't work as all it says in the error logs is
" The best overloaded method match for `string.Contains(string)' has some invalid arguments"
any help would be appreciated. :D
when posting code, please format it properly.
you need to do the comparison the other way round. something like:
if (Enemys.Contains(hit.transform.name))
{
// code here
}
but Enemys
should probably be a List
Answer by WinterboltGames · Jun 21, 2017 at 09:52 AM
don't use arrays since they don't have a function to help you check if an array contains an element use Generic Lists Instead like this...
List<string> Enemies = new List<string>()
{
"Cube",
Sphere
}
if (Enemies.Contains(hit.transform.gameObject.name))
{
Debug.Log("Check!");
}
also, you need to be using System.Collections.Generic;
hope this helps
Your answer
Follow this Question
Related Questions
C# If string is equal to any in array 2 Answers
Wher is the problem? My string isn't behaving... 1 Answer
Trying to spawn enemies on only one path 1 Answer
Access to objects using string 0 Answers
Error CS1061: Are you missing an assembly reference? 2 Answers