0

文字列の配列で部分文字列を検索しようとしています。私は次のコードを使用しています(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;
       }
    }
}

問題は、Debug.Logにが表示されることBoundary(Clone)です。Boundary配列に含めましたobstacles。以下のコードfrontAvailableをfalseに設定するべきではありませんか?それとも私はここで間違いを犯しましたか?

4

2 に答える 2

1

I think you need indexOf, not IndexOf. Assuming you're talking about the native string function.

In addition, indexOf returns -1 if there is no match, 0 if the match is at the start, 1, 2, 3... for further positions. So you need > -1 instead of > 0

于 2013-01-25T20:23:27.707 に答える
1

Kolinkの答えに加えて、あなたはその逆ではなく、の先頭をif探しています。私はあなたが探していると思います:Boundary(clone)Boundary

if (hitFront.collider.gameObject.name.IndexOf(obstacles[i]) >= 0)
于 2013-01-25T20:26:30.290 に答える