- Home /
 
Modifying a variable outside of player object.
Hi, im trying to create a 2D multiplayer game. I have this bool isBoosted changed to true when it collides with something like some sort of a speed up.
Here is the script that modifies it.
 public class MultiPowerUpRun : MonoBehaviour {
 
     // Use this for initialization
     public float boostTime;
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     public void OnTriggerEnter2D(Collider2D other){
         if (other.tag == "Player") {
             other.GetComponent<PlayerManager>().isBoosted = true;
             if(GameController.control.speedlvl == 0){
                 boostTime = 4f;
             }
             if(GameController.control.speedlvl == 1){
                 boostTime = 4.5f;
             }
             if(GameController.control.speedlvl == 2){
                 boostTime = 4.7f;
             }
             if(GameController.control.speedlvl == 3){
                 boostTime = 5f;
             }
             if(GameController.control.speedlvl == 4){
                 boostTime = 5.5f;
             }
             if(GameController.control.speedlvl == 5){
                 boostTime = 6f;
             }
             //other.GetComponent<PlayerManager>().resetIsBoosted(boostTime);
             Destroy(gameObject);
         }
     }
 }
 
               Now here in my playerscript. I print out the value of isBoosted and my speedX. Here is the script
 public class PlayerManager : NetworkBehaviour{
     static float speedX;
     static float speedY;
     static bool facingright;
     static bool isShooting;
     static Animator anim;
     static Rigidbody2D myBody;
 
     public bool isBoosted;
 
     void Update (){
         Debug.Log ("Boosted: " + isBoosted + " SpeedX: " + speedX);
 
         if (isLocalPlayer) {
             if(isAlive){
                 anim.SetBool("isAlive",true);
                 MovePlayer (speedX);
                 ariusManager(meterCount);
             if (facingright && speedX < 0 || !facingright && speedX > 0) {
                     facingright = !facingright;
                     flipHere ();
                     CmdSendFlip (transform.localScale.x);
                 }
                 anim.SetFloat ("verticalSpeed", myBody.velocity.y);
                 anim.SetBool ("isShooting", isShooting);
                 if (isShooting) {
                     Invoke ("resetShooting", 0.4f);
                 }
             }
             else{
                 anim.SetBool("isAlive",false);
             }
             LocalPop();
         }
     }
 
     void MovePlayer(float playerSpeed){
         myBody.velocity = new Vector3 (playerSpeed, myBody.velocity.y, 0);
     }
 
 
 
     public void WalkLeft(){
         if (!isAlive) {
             return;
         }
 
         if (isBoosted) {
             speedX = -20;
         } else {
             speedX = -9;
         }
         anim.SetBool ("Speed", true);
         pumpRelease ();
     }
     public void WalkRight(){
     if (!isAlive) {
         return;
     }
     if (isBoosted) {
         speedX = -20;
     } else {
         speedX = -9;
     }
     anim.SetBool ("Speed", true);
     pumpRelease ();
 }
 
 
 
               when the value of isBoosted is changed to true. I was expecting to see in the console "Boosted: True SpeedX: 20 or -20" but instead i still get "Boosted: True SpeedX: 9 or -9"
here are some screen shots i took. This is before picking up the power up. https://postimg.org/image/u6736gpbr/
This is after picking it up https://postimg.org/image/cevgs09x3/
Hope you can help me guys :(
Answer by UnityCoach · Dec 09, 2016 at 12:23 PM
Well, the only methods that change those values are WalkRight and WalkRight, and I don't see them called anywhere.
I'd suggest you use a property instead of just a variable for this kind of mechanics. Like :
 private bool _isBoosted;
 public bool IsBoosted
 {
 get { return _isBoosted; }
 set
 {
     if (_isBoosted != value)
     {
         if (_isBoosted)
         {
             speedX = -20;
         }
         else
         {
             speedX = -9;
         }
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
UWP and HLAPI 0 Answers