![](/img/trans.png)
[英]Unity3D if condition with bool failing to send variable value to another script c#
[英]Unable to get value of public variable into another script in Unity3D c#
我的gameController.cs
脚本中有一个public Color winColor
var。 我在Start()
设置它的值。 现在我想在另一个脚本check.cs
获取它的值。
现在我因为它是公开的,所以我使用了GameObject.Find("gameController").GetComponent<gamePlay>().winColor;
这里的问题是它显示了不同的值。 这是我在tile.cs
代码
private Color winingColor;
void Start ()
{
winingColor = GameObject.Find("gameController").GetComponent<gamePlay>().winColor;
Debug.Log(winingColor);
}
void Update ()
{
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
bool overSprite = this.GetComponent<SpriteRenderer>().bounds.Contains(mousePosition);
if (overSprite)
{
if (Input.GetButton("Fire1"))
{
if (this.GetComponent<SpriteRenderer>().color == winingColor)
{
float x = this.gameObject.transform.position.x;
this.gameObject.transform.position = new Vector3(x, 3.5f, 0.0f);
}
}
}
}
gameController.cs
代码
public GameObject ball;
public List<GameObject> tiles;
private Color [] colors = { new Color(0,1,0,1), new Color(1,0,0,1), new Color(1,1,1,1), new Color(0,0,1,1), new Color(1,1,0,1), new Color(0, 0, 0, 1)};
public Color winColor;
// Use this for initialization
void Start ()
{
winColor = colors[1];
Debug.Log("con wincolor:" + winColor);
ball.GetComponent<SpriteRenderer>().color = colors[1];
tiles[0].GetComponent<SpriteRenderer>().color = colors[0];
tiles[1].GetComponent<SpriteRenderer>().color = colors[1];
tiles[2].GetComponent<SpriteRenderer>().color = colors[3];
tiles[3].GetComponent<SpriteRenderer>().color = colors[4];
}
winColor
中gameController.cs
的值是RGBA(1.000, 0.000, 0.000, 1.000)
但是在tile.cs
我得到RGBA(0.000, 0.000, 0.000, 0.000)
有什么想法吗?
不同游戏对象的 Start() 以您不期望的顺序发生。
如果 Tile.cs Start() 首先发生,则不会设置 winColor。
将此行移至 Awake()
winColor = colors[1];
您可以解决此问题的另一种方法,如果不应该更改 winColor,则您可以将 winColor 更改为 Property getter,并删除winColor = colors[1];
线。
public Color winColor { get { return colors[1];}}
在我的实验中它是有效的,你确定你没有改变代码中某处的颜色吗?
测试2:
public class test2 : MonoBehaviour
{
public Color winColor;
private Color[] colors = { new Color(0, 1, 0, 1), new Color(1, 0, 0, 1), new Color(1, 1, 1, 1), new Color(0, 0, 1, 1), new Color(1, 1, 0, 1), new Color(0, 0, 0, 1) };
// Start is called before the first frame update
void Start()
{
winColor = colors[1];
Debug.Log("con wincolor:" + winColor);
}
}
测试1:
public class test1 : MonoBehaviour
{
private Color winingColor;
// Start is called before the first frame update
void Start()
{
winingColor = GameObject.Find("test").GetComponent<test2>().winColor;
Debug.Log("test1:" + winingColor);
}
}
另外在我看来,最好使用Getter/Setter来访问公共属性,它们为您提供了更好的灵活性:
public class test2 : MonoBehaviour
{
private Color[] colors = { new Color(0, 1, 0, 1), new Color(1, 0, 0, 1), new Color(1, 1, 1, 1), new Color(0, 0, 1, 1), new Color(1, 1, 0, 1), new Color(0, 0, 0, 1) };
public Color winColor
{
get; set;
}
// Start is called before the first frame update
void Start()
{
winColor = colors[1];
Debug.Log("con wincolor:" + winColor);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.