- Home /
Network call Method from Instants of Prefab
I have a project like this:
void Update() {
CmdMethod(22);
}
[Command]
private void CmdMethod(int i) {
GameObject clone = (GameObject) Instantiate(Prefab, new Vector3(1,1,1), Quaternion.identity);
clone.GetComponent<Script>().Init(i);
Rigidbody rigBody = clone.GetComponent<Rigidbody>();
rigBody.AddForce(playerDirection.normalized * impuls, ForceMode.Impulse);
NetworkServer.Spawn(clone);
}
When i run this on the server client all is fine and it works but when i run it on a normal client the Init() Methode is not called. How can i fix this? The stupit thing is the Rigidbody stuff works and i dont understand the differenc ... and the object is spawned and all works normal but without called Init() (Init chance some Material and renderer stuff on the game Object)
Answer by TheSorm · May 06, 2016 at 11:38 AM
void Update() {
CmdMethod(22);
}
[Command]
private void CmdMethod(int i) {
GameObject clone = (GameObject) Instantiate(Prefab, new Vector3(1,1,1), Quaternion.identity);
Rigidbody rigBody = clone.GetComponent<Rigidbody>();
rigBody.AddForce(playerDirection.normalized * impuls, ForceMode.Impulse);
NetworkServer.Spawn(clone);
clone.GetComponent<Script>().RpcInit(i);
}
This is how it works first spawn the Prefab on all Clients and than send the Spawned Objects via RPC call the right Methiod call.
Answer by Munchy2007 · May 05, 2016 at 11:54 PM
The function has the [Command] attribute, it can only ever run on the server.
If you want something to run on all connected clients, you need to make an RPC method and invoke it from the server.
Yes i know but thats not an anser for my question ... when i instatiate something in a [Command] $$anonymous$$ethode it is only on the server so when i call some stuff in that game object i think it shuld be done on the server and spawned to all clients with NetworkServer.Spawn ?!
Your answer
Follow this Question
Related Questions
Non player objects movements are not syncronized on server 0 Answers
Trying to send command for object without authority. - change color of object from client 0 Answers
Multiplayer desynchronize when grabbing an object by code. 0 Answers
SyncListString not changing in a Command 0 Answers
Trying to send command for object without authority - on a networkidenity with local authority 2 Answers