Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by legitplayer09 · Dec 09, 2016 at 06:18 AM · c#multiplayer

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"

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image legitplayer09 · Dec 08, 2016 at 11:37 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

UWP and HLAPI 0 Answers

Loading spawned prefabs upon connecting to server? 1 Answer

Making a bubble level (not a game but work tool) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges