Search for a substring in an array of strings in units

I am trying to find a substring in an array of strings. I use the following code (in Unity3):

var obstacles = ["Border", "Boundary", "BoundaryFlame"];
var frontAvailable = true;
var leftAvailable = true;
var rightAvailable = true;
var hitFront: RaycastHit;
if (Physics.Raycast(transform.position, transform.position + transform.forward, hitFront, 1.5)) {
    Debug.Log("I hit this in front: ");
    Debug.Log(hitFront.collider.gameObject.name);
    for (var i = 0; i < obstacles.length; i++)
    {
       if (obstacles[i].IndexOf(hitFront.collider.gameObject.name) > -1)
       {
          Debug.Log("Hit in front!");
          frontAvailable = false;
       }
    }
}

The problem is what Debug.Log is showing Boundary(Clone). I included Boundaryin the array obstacles. Should the code below be frontAvailableset to false? Or did I make a mistake here?

0
source share
2 answers

In addition to Kolink's answer, yours is iflooking Boundary(clone)at the beginning Boundary, not the other way around. I think you are looking for:

if (hitFront.collider.gameObject.name.IndexOf(obstacles[i]) >= 0)
+1
source

I think you need indexOf, not indexOf. Assuming you are talking about your own string function.

, indexOf -1, , 0, , 1, 2, 3... . > -1 > 0

+1

All Articles