将int值从一个脚本传递到另一个脚本

时间:2016-01-16 21:54:33

标签: unity3d unityscript

我正在尝试将public int score值从一个脚本传递到另一个脚本,但它给了我错误an object reference is required to access non-static member,这就是我所做的

public class firearrow : MonoBehaviour {
    public GameObject Arrow;
    public GameObject apple;
    public int score = 0;


    // Use this for initialization
    void Start () {
        this.gameObject.GetComponent<Rigidbody2D> ().AddForce (transform.right*1500.0f);

    }


    // Update is called once per frame
    void Update () {
        Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        diff.Normalize();

        float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 0);

        if (Input.GetMouseButtonUp (0)) {


            GameObject bullet_new;

            bullet_new = Instantiate (Arrow,new Vector2 (-0.23f, -3.78f), Quaternion.identity) as GameObject;
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),Vector2.zero);
            if (hit.collider!= null ) {

                LeanTween.move(bullet_new,  hit.collider.transform.localPosition, 1);
                if(hit.collider.tag == "fruit")
                {           
                    score++;
                    Destroy(hit.collider.gameObject,1);
                    Destroy(bullet_new,1);
                }

            }


        }
    }




}

我想要访问分数的课程

public class tick : MonoBehaviour {
    public Text wintext;
    // Use this for initialization
    void Start () {
        wintext.text = "";
    }

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

        if (Input.GetMouseButtonUp (0)) {

           if(firearrow.score == 3)
            {
                wintext.text="You Win";

            }


           }

    }
       }

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

更改行

adb shell dumpsys battery unplug
adb shell dumpsys deviceidle step

public int score = 0;

请注意,您必须只有一个类firearrow实例,否则可能会遇到并发问题。