在unityscript中的字符串数组中搜索子字符串 [英] Search a substring in an array of Strings in unityscript

查看:564
本文介绍了在unityscript中的字符串数组中搜索子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在字符串数组中搜索子字符串.我在Unity3中使用以下代码:

I'm trying to search a substring in an array of Strings. I'm using 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;
       }
    }
}

问题是,Debug.Log显示Boundary(Clone).我已经将Boundary包含在数组obstacles中.下面的代码不应该将frontAvailable设置为false吗?还是我在这里犯了错误?

The problem is, the Debug.Log shows Boundary(Clone). I've included Boundary in the array obstacles. Shouldn't below code set frontAvailable to false? Or did I make a mistake here?

推荐答案

除了Kolink的答案,您的if还在Boundary的开头寻找Boundary(clone),而不是相反.我认为您正在寻找:

In addition to Kolink's answer, your if is looking for Boundary(clone) at the beginning of Boundary, rather than the other way around. I think you're looking for:

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

这篇关于在unityscript中的字符串数组中搜索子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆