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