• 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
Question by Javimillan · May 20, 2017 at 08:57 AM · networkingplayernetworkvariables

Help with player variables (prefab/network)

Hi guys, i'm doing a multiplayer game and i have some problems when i try to access to the other player variables. I can't read anyt$$anonymous$$ng of t$$anonymous$$s variables cause their returns me 0 always.

T$$anonymous$$s is caused because i have to configure it with [clientrpc] or network.rpc? How i can get the varibles of the other player? I don't understand it, someone can explain me how i should do it?

There is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 
 public class MovementMulti : NetworkBehaviour {
 
     private float aumentoA = 0.1f;
     [Header("Movement Variables")]
     [SerializeField]
     public float vX;
     [SerializeField]
     public float vZ;
     [Header("Camera Position Variables")]
     [SerializeField]
     float cameraHeight = 15f;
     Rigidbody rg;
     public int id;
     bool bounced = true;
     public float rX;
     public float rZ;
     public float potencia;
     
 
     Transform mainCamera;
     Vector3 cameraOffset;
     // Use t$$anonymous$$s for initialization
     void Start()
     {
         if (isLocalPlayer) {
             cameraOffset = new Vector3(0f, cameraHeight, 0f);
             mainCamera = Camera.main.transform;
             MoveCamera();
             rg = GetComponent<Rigidbody>();
             id = Random.Range(0, 1000000000);
         }
 
     }
 
     void MoveCamera() {
         mainCamera.position = transform.position;
         mainCamera.rotation = transform.rotation;
         mainCamera.Translate(cameraOffset);
         mainCamera.LookAt(transform);
     }
     
     
     private void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.tag == "Player")
         {
             Debug.Log(t$$anonymous$$s.gameObject.name);
             Debug.Log(other.gameObject.name);
             if (id > other.gameObject.GetComponent<MovementMulti>().id && bounced) // <--IT RETURNS 0
             {
                 bounced = false;
                 float aux = vZ;
                 float aux2 = vX;
                 vZ = other.gameObject.GetComponent<MovementMulti>().vZ;//IT RETURNS 0
                 vX = other.gameObject.GetComponent<MovementMulti>().vX; //IT RETURNS 0
                 other.gameObject.GetComponent<MovementMulti>().vZ = aux;
                 other.gameObject.GetComponent<MovementMulti>().vX = aux2;
             }
         }
     }
 
     private void OnCollisionExit(Collision other) {
         bounced = true;
     }
 
     // Update is called once per frame
     void Update()
     {
         float x = Input.acceleration.x; //x negativa izquierda
         float y = Input.acceleration.y; //y negativa abajo
 
         float aumentoX = aumentoA * x;
         float aumentoY = aumentoA * y;
 
         if (x > 0)
         {
             vX += aumentoX;
         }
         else if (x < 0)
         {
             vX += aumentoX;
         }
 
         if (y > 0)
         {        // Up
             vZ += aumentoY;
         }
         else if (y < 0)
         {        // Down
             vZ += aumentoY;
         }
 
         //Dependiendo de los valores del eje se moverá de una forma u o de otra.
         transform.Translate(Vector3.right * vX * Time.deltaTime);
         transform.Translate(Vector3.forward * vZ * Time.deltaTime);
         MoveCamera();
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by XxSimBaaxX · May 22, 2017 at 07:27 AM

I am not that good in terms of scripting, but I t$$anonymous$$nk your problem is that the variable's values only change at the local player only and are not synchronizing with the host and then to the clients, you probably have to add the [SyncVar] at every variable you want to synchronize with the host and then add a a [ClientRpc] function that is called every frame and sync the values again but now from the host to the rest of the players, that will create a loop w$$anonymous$$ch first syncs the variables from the local player to the host and then from the host back to the rest of the clients. note that you cant call [ClientRpc] on the local player, you can only call it from the host (you can add an if statement for isServer is true). sorry if its a bit complicated I am bad at explaining xD

Comment

People who like this

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiplayer|3dText PlayerName 1 Answer

What is the CCU of the default Unity Networking? 1 Answer

How to pass a variable to a late-joining player without syncvars? / What function gets called on a new player connect? 1 Answer

IS it possible to have delayed player spawning after server connection? (UNet) 1 Answer

Network Game Player reference 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