Issues sharing variable between 2 scripts

its been i while since ive used unity (or coded in general) and ive forgotten how to share a variable between 2 script. i have 2 scripts they both have a variable called underChunkX my first script updates the variable frequently my second script need this variable, ive look up tutorials but nothing ive tried works, my second script with the variable returns 0

My first script (variables home):

	public float underChunkX;

void OnCollisionEnter(Collision col){
		underChunkX = col.gameObject.transform.position.x;
	}

My second script(where i need the variable):

void Update(){
		underChunkX = player.GetComponent<Player> ().underChunkX;
		Debug.Log (underChunkX);
	}

Note “player” is the gameobject that the “Player” class is attached to

i fixed it, for some reason if i moved it to a different function everything worked fine

I don’t think you want a GetComponent<>() in Update(). It’s a relatively expensive process to have running 50 or so times a second. Best to get that in Start(). Assuming the value itself isn’t changing 50 times a second also!