hello , i know this question has been asked but i haven’t got any perfect answer yet
so , i have a script called “Score” which has an int variable called CurrentScore = 0
now i have another script which i want it to get that component and the variable “CurrentScore” then on a Collision even ( OnCollisionEnter ) it access the variable “CurrentScore” and changes it to 1++ so every collision it adds 1 . thanks everybody and btw im using C#
oh one more thing , i also have created another gameobject which contains a script called “GlobalTimer” which has an int variable called Timer (its a count down timer) i also want with the collision event above to add lets say (int) more 3 seconds to the timer and thanks again
so , i know how to get a component but the problem is i dont know how to get the variable and add a 1++ to it
For Accessing any member of class in other class you can do like that.
public class Score : MonoBehaviour {
public static Score instace;
public int CurrentScore;
// Use this for initialization
void Start () {
instace = this;
}
}
Now you Can simply access Current Score in any script like
MoveTo MT = gameObject.GetComponent ();
MT.enabled = false;
this is the prettiest way " MT" will be local this way and wont interfere with other scripts in the same class.
scriptName SN = gameObject.GetComponent<scriptName> ();
SN.var= value;
I simply don’t know why this doesn’t work in my code. I have a “Motorcycle” object with a “BackWCollider” child object which has a “MoveBike” script attached to it.