- Home /
Question by
Albireo · Feb 16, 2014 at 04:01 AM ·
Unity cannot correctly refer another variable in another script?
public class nyancatcontrol : MonoBehaviour {
public float force=10;
public int scores=0;
public bool starts=false;
public gameControl time;
float times=1,timer=0;
// Use this for initialization
void Awake(){
times=time.time;
timer=times;
}
void Start () {
starts=false;
transform.position=new Vector2(0,0);
rigidbody2D.isKinematic=true;
}
// Update is called once per frame
void Update () {
print(starts);
jump();
score();
}
void OnCollisionEnter2D(Collision2D other){
Destroy(renderer);
print("hit");
}
void jump(){
if(Input.GetMouseButtonDown(0)||Input.touchCount==1){
rigidbody2D.isKinematic=false;
rigidbody2D.velocity=new Vector2(0,0);
rigidbody2D.AddForce(Vector2.up*force);
}
}
void score(){
if(starts==true){
print ("a");
timer-=Time.deltaTime;
if(timer<=0){
scores+=1;
print (scores);
timer=times;
}
}
}
}
public class mapControl : MonoBehaviour {
public float speed=10f;
public Transform nyancat;
// Use this for initialization
void Awake(){
}
void Start () {
}
// Update is called once per frame
void Update () {
print(nyancat.GetComponent<nyancatcontrol>().starts);
transform.Translate(Vector3.left*speed*Time.deltaTime);
if(transform.position.x<-20){
GameObject.Destroy(gameObject);
}
if(transform.position.x<0){
// nyancat.GetComponent<nyancatcontrol>().starts=true;
}
}
}
If i print starts in nyancat control it is always false, and if i print it in mapcontrol it is always true. How can accessing the same variable can have different values? Dosent make sense at all?!
Comment