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

时间:2015-03-23 03:21:00

标签: c# unity3d

我正在尝试在我的第一个脚本中调用重叠,当我在自己的脚本中使用debug.log时,它可以完美地工作并提供所需的结果但是当我尝试调用它时它总是返回false。

第一个脚本:

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

1 个答案:

答案 0 :(得分:0)

您需要在实例上调用方法而不是“模板”预制件。

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上的方法用作其唯一用于创建实例的模板预制件。