Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Desync404 · Jun 25, 2015 at 12:50 PM · errorunity5

Using uNet to convert from single player to multiplayer - Where to assign SyncVar, Client and Command?

// Blurb begins

For the last few days I've been working on a single-player version of what will eventually be a multiplayer game. The game is a sandbox RPG (ugh) like ARK, Rust, whatever. In the last few days I've completed the systems for player skills, health /other stats, equipped and non-equipped items, crafting, harvesting and picking up items from the game world.

With all but item placement, combat and actual survival elements implemented (excl. the whole host of models and a fully-furnished game world...) I decided to give multiplayer code a shot before things became too messy, so that I could continue to implement features in a multiplayer project rather than a single-player project. For now, I'm looking to make a "working prototype" - something that myself and a few friends could play on to demonstrate how the game is played. For this reason I've decided to use uNet, since I'm lazy af and didn't want to download a better networking solution at the time.

I got players and spawning set up with no issues (not very complicated since it was mostly drag and drop!), and the destructible game world (ie. trees falling over when harvested) registers across the server. All was going well.

// Blurb ends


// Problem

The issue that I have is that, once there's more than one person on a server (ie. someone other than the host) I get a NullReferenceException stating that the PlayerStats script doesn't exist. Here's a rough overview of how things are set up at the moment:

Database GameObject

Item Database: contains List with all items used in the game by players

Perk Database: contains List with all perks that players have access to

Recipe Database: contains List with all crafting recipes

Player GameObject

Actions (Script): contains a Raycast to find what GameObject is hit; does various things depending on the tag of the GameObject

Inventory (Script): contains List which stores all objects in the player's inventory, as well as the OnGUI() to draw the inventory on screen. There are functions such as AddItem(), RemoveItem(), EquipItem() and so forth in here.

PlayerStats (Script): contains List, List, List. All of these are created and added to by functions called InitialisePlayerSkill/Vital/Equipment in the Start() method. Also on this script is the function for unequipping an item.

Crafting (Script): contains the OnGUI()function for the crafting window, which links back around to the player's inventory script to Add /Remove items.

As I said, I get a NullReferenceException when a second player joins the world:

"NullReferenceException: Object reference not set to an instance of an object PlayerStats.Update () (at Assets/Resources/Scripts/PlayerStats.cs:46)"

 void Update() {
     if(!isLocalPlayer)
     {
         // exit from update if this is not the local player
         return;
     }

     if(Input.GetButtonDown("Character Pane"))
     {
         showCharPane = !showCharPane;
     }

     foreach(Vital v in playerVitals) // OFFENDING LINE (#46)
     {
         if(v.curValue <= v.maxValue && v.curValue >= 0)
         {
             v.curValue += v.vitalRegen * Time.fixedDeltaTime;
         }

         if(v.curValue > v.maxValue)
         {
             v.curValue = v.maxValue;
         }
     }
 }

The offending line (Line 46) is highlighted. This suggests to me that playerVitals doesn't actually exist to this player (player 2), confirmed by another NullReferenceException on line 29 (which simply changes the value at `playerVitals[0]'.

// Looking for a solution...

If I'm correct, this would be due to the new player immediately being out of sync with the server. At the moment, I'm (trying) to store the player's stats on the server. I all honesty, I have very little idea what I'm doing with regards to player variables...

Do I store all of the player's stats on the server or do I keep them on the client until they're needed somewhere else on the server (ie. damage detection or adding XP to a player's skill when they cut down a tree)? I know that the [SyncVar] tag can be added before a field's declaration at the start of a script, but what does this actually do? Does it store that instance of the script's version of the variable or does it make that variable a global value shared by all clients?

// More broadly...

As well as not really knowing when to use [SyncVar], I don't really know when to use [Client] and [Command] before my methods! How do I know, for certain, whether I need to give a method or a variable one of these tags?

Thanks in advance!

Comment
Add comment
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

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Unity 5 - submeshes don't work as before 1 Answer

Unity 5 Light Problem 1 Answer

Why don't shadows appear in my built .exe? Unity 5 4 Answers

Sound files not working [Unity 5.1] 0 Answers

Can't Play Animation 0 Answers


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