• 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 /
  • Help Room /
This question was closed May 15, 2016 at 02:24 AM by Prefab for the following reason:

I have found the following which largely answer my question: http://answers.unity3d.com/questions/740703/when-to-and-when-not-to-use-static-variables.html http://forum.unity3d.com/threads/why-is-static-considered-bad-practice.200363/

avatar image
Question by Prefab · May 14, 2016 at 12:14 PM · networkingmultiplayervariablereferencestatic

What are some best practices for static variables in multiplayer?

I have generally stayed away from using static variables because based on past research they are considered "evil" and should only be used as a last resort.

I am now working on a project involving a login system and the posting of user data (user name, scores etc) and storing it into a database. In the first scene, the login scene, the player username is stored into a variable and the gameobject is set to "dontdestroyonload". In the next scene, the game, several scripts on different gameobjects need to make reference to this script in order to do their job; eg. different UI text elements need to display different user data, the score manager GO needs to be able to post the user data, AI agents need to react/rubberband to the player based on how the player is doing etc etc.

What I am getting at is that there are a tonne of references that need to made for each case and it seems a little silly to be doing this over and over again. The fact that the script initially only exists in the very first scene also complicates this, since FindGameObjectsWithTag needs to be used each time and the one script isolated; all extra unnecessary computation in my opinion. It also seems easier to simply declare user data such as username, score, health as static in one script (since it is always going to be consistent) and then it can easily be called from any script anywhere. I am concerned though, whether using statics can cause any issues in a multiplayer environment, since I will be coding this next, and I am trying to code with this in mind.

So in short:

Static variables for multiplayer; when are they ok to use and what are some common pitfalls to look out for?

Comment

People who like this

0 Show 0
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

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

74 People are following this question.

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

Related Questions

Spawning GameObject on network, locally change it only on the client that spawned it. 0 Answers

UNET - syncvars 2 Answers

Help with Photon Networking Dodge Ball Game 0 Answers

Help with connecting computers 1 Answer

Failed to spawn server object, assetid=... 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