C# 在Unity C中从另一个脚本调用非静态变量
我试图在我的第一个脚本中调用overlapping,当我在它自己的脚本中使用debug.log时,它工作得很好,并提供了所需的结果,但当我尝试调用它时,它总是返回false 第一个脚本:C# 在Unity C中从另一个脚本调用非静态变量,c#,unity3d,C#,Unity3d,我试图在我的第一个脚本中调用overlapping,当我在它自己的脚本中使用debug.log时,它工作得很好,并提供了所需的结果,但当我尝试调用它时,它总是返回false 第一个脚本: public class AscendingSquareScript : MonoBehaviour { public GameObject ascendingBlock; SquareBlockScript squareBlockScript; float playerWidth;
public class AscendingSquareScript : MonoBehaviour {
public GameObject ascendingBlock;
SquareBlockScript squareBlockScript;
float playerWidth;
float playerHeight;
float xPlacement;
GameObject ascendingSquareHolder;
public KeyCode moveDown;
GameObject block;
public SquareBlockScript value;
void Start() {
squareBlockScript = ascendingBlock.GetComponent<SquareBlockScript> () as SquareBlockScript;
ascendingSquareHolder = GameObject.FindGameObjectWithTag ("AscendingSquareHolder");
playerHeight = ascendingBlock.gameObject.renderer.bounds.size.y;
playerWidth = ascendingBlock.gameObject.renderer.bounds.size.x;
for (int i = 0; i < 6; i++) {
RandomNumberGenerator ();
block = Instantiate (ascendingBlock) as GameObject;
block.transform.position = new Vector3(Camera.main.ScreenToWorldPoint(new Vector3(0f,0f,0f)).x + (playerWidth/2) + (playerWidth * xPlacement), Camera.main.ScreenToWorldPoint(new Vector3(0f,0f,0f)).y + playerHeight/2 + (playerHeight*i),0f);
block.transform.parent = ascendingSquareHolder.transform;
}
}
void Update () {
if (ascendingBlock.GetComponent<SquareBlockScript> ().getOverLap ())
Debug.Log ("It is currently working");
if (Input.GetKeyUp (moveDown)) {
ascendingSquareHolder.transform.position = ascendingSquareHolder.transform.position + new Vector3(0f, -playerHeight, 0f);
GeneratingSquares();
}
}
}
您需要调用实例上的方法,而不是模板预置
public class AscendingSquareScript : MonoBehaviour {
public GameObject ascendingBlock;
SquareBlockScript squareBlockScript;
float playerWidth;
float playerHeight;
float xPlacement;
GameObject ascendingSquareHolder;
public KeyCode moveDown;
GameObject block;
public SquareBlockScript value;
void Start() {
squareBlockScript = ascendingBlock.GetComponent<SquareBlockScript> () as SquareBlockScript;
ascendingSquareHolder = GameObject.FindGameObjectWithTag ("AscendingSquareHolder");
playerHeight = ascendingBlock.gameObject.renderer.bounds.size.y;
playerWidth = ascendingBlock.gameObject.renderer.bounds.size.x;
for (int i = 0; i < 6; i++) {
RandomNumberGenerator ();
block = Instantiate (ascendingBlock) as GameObject;
block.transform.position = new Vector3(Camera.main.ScreenToWorldPoint(new Vector3(0f,0f,0f)).x + (playerWidth/2) + (playerWidth * xPlacement), Camera.main.ScreenToWorldPoint(new Vector3(0f,0f,0f)).y + playerHeight/2 + (playerHeight*i),0f);
block.transform.parent = ascendingSquareHolder.transform;
}
}
void Update () {
if (block.GetComponent<SquareBlockScript> ().getOverLap ())
Debug.Log ("It is currently working");
if (Input.GetKeyUp (moveDown)) {
ascendingSquareHolder.transform.position = ascendingSquareHolder.transform.position + new Vector3(0f, -playerHeight, 0f);
GeneratingSquares();
}
}
}
所以在块上调用.getOverLap而不是ascendingBlock。但这仅适用于分配给块的最后一个实例。您不能在ascendingBlock上使用该方法,因为它只是用于创建实例的模板预制件。因此您确实看到了来自SquareBlockScript.OnTriggerEnter2D的“it is”消息,但您没有得到“it is current working”消息?是否有办法将同一变量应用于它的多个实例,而不仅仅是最后一个?在实例化它们时,您必须将它们放入数组或列表中。
public class AscendingSquareScript : MonoBehaviour {
public GameObject ascendingBlock;
SquareBlockScript squareBlockScript;
float playerWidth;
float playerHeight;
float xPlacement;
GameObject ascendingSquareHolder;
public KeyCode moveDown;
GameObject block;
public SquareBlockScript value;
void Start() {
squareBlockScript = ascendingBlock.GetComponent<SquareBlockScript> () as SquareBlockScript;
ascendingSquareHolder = GameObject.FindGameObjectWithTag ("AscendingSquareHolder");
playerHeight = ascendingBlock.gameObject.renderer.bounds.size.y;
playerWidth = ascendingBlock.gameObject.renderer.bounds.size.x;
for (int i = 0; i < 6; i++) {
RandomNumberGenerator ();
block = Instantiate (ascendingBlock) as GameObject;
block.transform.position = new Vector3(Camera.main.ScreenToWorldPoint(new Vector3(0f,0f,0f)).x + (playerWidth/2) + (playerWidth * xPlacement), Camera.main.ScreenToWorldPoint(new Vector3(0f,0f,0f)).y + playerHeight/2 + (playerHeight*i),0f);
block.transform.parent = ascendingSquareHolder.transform;
}
}
void Update () {
if (block.GetComponent<SquareBlockScript> ().getOverLap ())
Debug.Log ("It is currently working");
if (Input.GetKeyUp (moveDown)) {
ascendingSquareHolder.transform.position = ascendingSquareHolder.transform.position + new Vector3(0f, -playerHeight, 0f);
GeneratingSquares();
}
}
}