Instantly pass other script's variable value to another.

Hi, I’m trying to pass a variable value from one script to another, but it seems that it’s passing it too late or I may have other problems. I’m still learning Unity and C# so forgive my ignorance :). Anyway, I’m trying to make one code that will start a “power meter” when you press and hold Mouse1, and another code that will make the character jump when you release Mouse1, depending on the value of the “power meter”.

Here’s the code attached to my power meter.

	void Update () {

		if (Input.GetMouseButtonDown(0) && (startMeter == 0)) {
			barStart = true;
			startMeter = 1;
		}
		if (Input.GetMouseButtonUp(0) && (startJump == 0) && barStart) {
			barStart = false;
			startJump = 1;
		}
		if(barStart && !maxJump){
			jumpPower += Time.deltaTime * barSpeed;
			jumpPower = Mathf.Clamp(jumpPower, minWidth, fullWidth);
			powerBar.pixelInset = new Rect (transform.localPosition.x, transform.localPosition.y, 20, jumpPower);
			if(jumpPower == 256){
				maxJump = true;
			}
		}
		if(barStart && maxJump){
			jumpPower -= Time.deltaTime * barSpeed;
			jumpPower = Mathf.Clamp(jumpPower, 0, fullWidth);
			powerBar.pixelInset = new Rect (transform.localPosition.x, transform.localPosition.y, 20, jumpPower);
			if(jumpPower == 0){
				maxJump = false;
			}
		}
		
	}

And here’s the other one attached to the player:

public PowerMeter jumpForce;

	void Update () {

		if (Input.GetMouseButtonUp(0) && grounded && (jumpForce.startJump == 1)) {
			GetComponent<Rigidbody2D>().velocity = new Vector3(0,0,0);
			GetComponent<Rigidbody2D>().AddForce (new Vector2(-3,(jumpForce.jumpPower/7)), ForceMode2D.Impulse);
			GetComponent<Rigidbody2D>().mass = 2;
			Destroy(GameObject.FindWithTag("PowerMeter"));
			//jumpForce.startJump = 0;
		}

whenever I let go of the Mouse1, the player would ignore the value set by the power meter (jumpPower variable). but when I press and release mouse1 again, the player would finally jump based on the value in the jumpPower.

I’m stuck, any help is appreciated.

Thanks :slight_smile:

it would be better to call a jump function on the player from the powermeter’s script instead of using the update twice. its more reliable and you can pass parameters.

var playerObject : gameObject

function Update(){
  var playerScript = playerObject.GetComponent(jumpScript);

  if (Input.GetMouseButtonUp(0) && (startJump == 0) && barStart) {
    barStart = false;
    playerScript.forceJump(jumpPower);
  }
}

and add this to the player:

function forceJump(jumpPower : float)
{
   if (grounded)
   {
         GetComponent<Rigidbody2D>().velocity = Vector3(0,0,0);
         GetComponent<Rigidbody2D>().AddForce (Vector2(-3,(jumpPower/7)), ForceMode2D.Impulse);
         GetComponent<Rigidbody2D>().mass = 2;
         Destroy(GameObject.FindWithTag("PowerMeter"));
   }
}

It might be a problem with the order of execution. Both functions are “Update” functions, and Unity will always execute one before the other. The problem is you can’t (and shouldn’t) force a certain order, so maybe the Update for your second script is runing before the Update from the first script.

You can try using a different update function, you also have FixedUpdate that runs before all Update functions, and LateUpdate that runs after all Update functions. Look here for more details: Unity - Manual: Order of execution for event functions