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

pass int value from one script to another script in unity

我试图将一个 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);
                }

            }


        }
    }




}

class 我想访问分数

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

            }


           }

    }
       }

有什么建议吗?

换行

public int score = 0;

public static int score = 0;

请注意,您必须只有一个 class firearrow 实例,否则您可能 运行 出现并发问题。