【发布时间】: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