C#/Unity - 我如何比较字符串变量?

C# / Unity - How I can compare string variable?

如何在 Unity 中比较第一个对象中的字符串变量和第二个对象中的字符串? 我的问题有截图:https://onedrive.live.com/redir?resid=C7A243435ECAB1C0!2948&authkey=!ADg0Uh6Ih1cJKd8&ithint=folder%2cpng

感谢您的回答。

玩家移动:

using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {

Vector3 pos;
public float speed;
char mv;
string ObstacleTypePlayer;


void Start () {

    pos = transform.position;

}

void Update () {

    if (Input.GetKeyDown(KeyCode.LeftArrow) && mv != 'l')
        pos.x -= speed;

    if (Input.GetKeyDown(KeyCode.RightArrow))
        pos.x += speed;

    if (Input.GetKeyDown(KeyCode.UpArrow) && mv != 'u')
        pos.y += speed;

    if (Input.GetKeyDown(KeyCode.DownArrow) && mv != 'd')
        pos.y -= speed;


    transform.position = pos;
}


void OnTriggerEnter2D(Collider2D col)
{

    if (col.CompareTag("obstacle"))
    {
        col.gameObject.GetComponent<Obstacle>().Move(true);
        print("obstacle");
    }

    if (col.gameObject.GetComponent<Obstacle>().ObstacleType == "big left") // not true, but if Obstacle.ObstacleType == "big left"
    {


        if (col.CompareTag("side_up"))
            NotMove('d');

        else if (col.CompareTag("side_down"))
            NotMove('u');

        else if (col.CompareTag("side_right"))
            NotMove('l');

    }

}

void OnTriggerStay2D(Collider2D col)
{

}


void OnTriggerExit2D(Collider2D col)
{
    if (col.CompareTag("side_up"))
        NotMove(' ');

    else if (col.CompareTag("side_down"))
        NotMove(' ');

    else if (col.CompareTag("side_right"))
        NotMove(' ');

}

char NotMove(char z)
{
    mv = z;
    return mv;
}

}

障碍:

Vector3 pos;
GameObject player;
bool move = false;
public string ObstacleType = "aaaa";

void Start () {
    player = GameObject.FindWithTag("Player");
    pos = transform.position;
}
public bool Move(bool mo)
{
    if(mo)
        move = true; 
    return move;
}

void Update () {



    if (ObstacleType == "big left" && Input.GetKeyDown(KeyCode.LeftArrow))
        move = false;

    if (move)
        transform.position = player.transform.position;

}

与您调用 Move() 函数的方式完全相同:

col.gameObject.GetComponent<Obstacle>().ObstacleType == "big left"

编辑:
据我所知,您的 Obstacle 游戏对象有一些子对象。他们每个人都有自己的标签(obstacleside_upside_downside_right)和碰撞器。由于上面的代码给了你一个 NullReferenceException,对我来说唯一可能的原因是只有主对象有 Obstacle.cs 组件,没有子对象,因此上面的行失败了。

编辑 2:
如果只有父级应该有脚本,该行应该是这样的:

col.transform.root.GetComponent<Obstacle>().ObstacleType == "big left"