【问题标题】:C# / Unity - How I can compare string variable? [closed]C# / Unity - 我如何比较字符串变量? [关闭]
【发布时间】:2016-05-11 19:00:36
【问题描述】:

如何比较 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;

}

【问题讨论】:

  • 将您的代码包含在问题中。您通常将字符串与== 进行比较。
  • obja.TheString == objb.TheString
  • 但与其他对象和脚本字符串比较
  • 由于问题似乎与该比较没有直接关系,因此包括整个类,而不仅仅是 sn-ps。
  • 在 Unity 中不同 GameObjects 中的脚本之间通信的唯一方法是使用“GameObject.SendMessage()” 在您的一个 GameObjects 的脚本中创建一个公共 GameObject 并将另一个 gameObject 附加到它的编辑。然后,将字符串发送到公共方法,例如:public void recieveString(string anyString)。现在你可以在另一个字符串中比较这个字符串了。

标签: c# string variables unity3d compare


【解决方案1】:

与您调用 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"

【讨论】:

  • 我知道,但这是错误:NullReferenceException: Object reference not set to an instance of an object Assets/Scripts/PlayerMove.cs 46
  • 我很久以前就试过了。 :D
  • 这是在其他脚本中访问变量的正确方法。如果您收到 NullReferenceException,则问题出在其他地方。
  • 但是在哪里,一切都是正确的:D
  • 您可能想在if(col.compareTag("obstacle")) 的末尾添加一个return; 以不进一步检查,或者将第二个块设为else if,因为碰撞对象只能有一个标签。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多