using UnityEngine;
using UnityEngine.Networking;
public class TestClass : NetworkBehaviour {
int x;
void Start()
{
if (isServer)
{
x = 1;
Debug.Log("x value: " + x); // x = 1
}
else
{
CmdTestCommand();
}
}
[Command]
void CmdTestCommand()
{
Debug.Log("x value: " + x); // x = 0
}
}
Obviously the debug.log says x is equal to 1 after initializing it.
However, after i create a room, and another client joins my server, my code is supposed to print that value (at least for now).
This does not work! Debug.Log says x is equal to zero in that case.
I think this is because even though x = 1 is assigned on the server, it is stored in the client object that is on the server, is this correct?
Is there a way to make my code work?
Thanks for your answer.
However, i tried both of your methods, and both seem not to work.
If i call Debug.Log(x) in the command, i still get a value of 0.
What am i doing wrong?
My Code:
public int x;
[SyncVar]
public int y;
void Start()
{
if (isServer)
{
x = 1;
y = 2;
}
else
{
CmdTestCommand();
}
}
[Command]
void CmdTestCommand()
{
Debug.Log("x: " + NetworkServer.FindLocalObject(GetComponent<NetworkIdentity>().netId).GetComponent<TestClass>().x); //Returns 0
Debug.Log("y: " + y); //Returns 0
}
Edit:
I found the solution myself.
I am disabling some scripts depending on if i am the local player or not.
I was also disabling this script for whatever reason, now that i figured that out everything works.