unity3d - pass int value from one script to another script in unity -
i'm trying pass public int score
value 1 script script giving me error an object reference required access non-static member
, here have done
public class firearrow : monobehaviour { public gameobject arrow; public gameobject apple; public int score = 0; // use initialization void start () { this.gameobject.getcomponent<rigidbody2d> ().addforce (transform.right*1500.0f); } // update 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) 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); } } } } }
the class want access score
public class tick : monobehaviour { public text wintext; // use initialization void start () { wintext.text = ""; } // update called once per frame void update () { if (input.getmousebuttonup (0)) { if(firearrow.score == 3) { wintext.text="you win"; } } } }
any suggestions?
change line
public int score = 0;
to
public static int score = 0;
note must have 1 single instance of class firearrow, otherwise might run concurrency issues.
Comments
Post a Comment