Changing a variable from one script to another C#

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 :slight_smile:

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

Score.instance.CurrentScore = 10; // Or Whatever

I explain it very clearly in these two topics: Transform var between scripts - Unity Answers

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;

sudo code ^

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.

Script in “Motorcycle”:

    private float ThrustIncrement;;
    private MoveBike MoveB;

    void start()
    {
        MoveB = transform.Find("BackWCollider").GetComponent<MoveBike>();
    }


    void OnTriggerEnter(Collider coll)
    {
        if (coll.gameObject.name == "NitroModel")
        {
            ThrustIncrement = 0.1f * MoveB.Thrust;
        }
    }

Script in “BackWCollider” (MoveBIke script):

public class MoveBike : MonoBehaviour {

    public float Thrust; //Wheel torque multiplier

...

I get this error in the ThrustIncrement setting line:

NullReferenceException: Object reference not set to an instance of an object
PowerUps.OnTriggerEnter (UnityEngine.Collider coll)