简体   繁体   English

在Unity C#中从另一个脚本调用非静态变量

[英]Calling a non static variable from another Script in Unity C#

I'm trying to call overlapping in my first script, it works perfectly and gives the desired result when I use a debug.log within it's own script but it always returns false when I try to call it. 我试图在第一个脚本中调用重叠,当我在自己的脚本中使用debug.log时,它可以完美工作并提供所需的结果,但是当我尝试调用它时,它总是返回false。

First Script: 第一个脚本:

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();
        }
    }
}

Second Script: 第二个脚本:

public class SquareBlockScript : MonoBehaviour {
    public bool overLapping = false;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {

    }
    void OnTriggerEnter2D(Collider2D col){
        if (col.gameObject.tag == "Player") {
            overLapping = true;
            Debug.Log (overLapping + " it is");

        }
        if (col.gameObject.tag == "LowerBound") {
            Destroy (this.gameObject);
        }
    }
    void OnTriggerExit2D(Collider2D col){
        if (col.gameObject.tag == "Player") {
            overLapping = false;
        }
    }
    public bool getOverLap(){
        return overLapping;
    }
}

You need to call the method on the instance not the "template" prefab. 您需要在实例上调用方法,而不是“模板”预制。

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();
    }
}
}

so call .getOverLap () on block not ascendingBlock. 因此请在不递增的块上调用.getOverLap()。 But this will only work for the last instance assigned to block. 但这仅适用于分配给block的最后一个实例。 You can't use the method on ascendingBlock as its only a template prefab used to create the instances. 您不能在ascendingBlock上使用该方法,因为它只是用于创建实例的模板预制板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM