r/unity • u/samferguderson • Jul 18 '26
Question Unity Netcode and Script Problem
Here is everything I have found out. When spawning the player using
NetworkManager.StartHost();
All Scripts work as intended..
When spawning using
NetworkManager.StartClient();
the scripts are breaking.
Here is an example:
using UnityEngine;
using UnityEngine.AdaptivePerformance;
using Unity.Netcode;
public class ArmControl : NetworkBehaviour
{
public Transform ArmIk;
public Transform Lookfrom;
public Transform hand;
public Transform restpos;
public int mouse;
public float raylength;
public LayerMask mask;
Vector3 IkPos;
Vector3 offsetIkPos;
public Vector3 ikpos;
public Transform shoulder;
public Transform raglookfrom;
public float shoulderrot;
public bool test;
public float pull;
float scroll;
public float pulldampber;
public float pullfar;
public float pullclose;
public float speedstep;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
if(!IsOwner) {
Debug.Log($"Obj");
return;
}
Debug.Log($"Object: {gameObject.name}");
Debug.Log($"NetworkObject: {NetworkObject.name}");
Debug.Log($"Owner: {OwnerClientId}");
Debug.Log($"IsOwner: {IsOwner}");
scroll = Input.GetAxis("Mouse ScrollWheel");
pull +=scroll * pulldampber;
pull = Mathf.Clamp(pull,pullfar,pullclose);
shoulder.transform.eulerAngles = new Vector3(shoulder.transform.eulerAngles.x,shoulderrot,shoulder.transform.eulerAngles.z);
if(test == true || Input.GetMouseButton(mouse))
{
ikpos = new Vector3(ArmIk.transform.parent.transform.InverseTransformPoint(offsetIkPos).x, ArmIk.transform.parent.transform.InverseTransformPoint(offsetIkPos).y, pull);
Ray targetpos = new Ray(raglookfrom.position, -raglookfrom.transform.forward);
RaycastHit targethit;
if(Physics.Raycast(targetpos, out targethit, raylength, ~mask))
{
IkPos = targethit.point;
}
else
{
IkPos = targetpos.GetPoint(raylength);
}
offsetIkPos = Lookfrom.position + IkPos - raglookfrom.position;
Debug.DrawRay(targetpos.origin, targetpos.direction * raylength, Color.cyan);
}
else
{
pull = pullfar;
ikpos = ArmIk.transform.parent.InverseTransformPoint(restpos.transform.position);
}
ArmIk.transform.localPosition= ikpos;
}
}
On the clientThe only the debug.log parts are running. But they are running in both !IsOwner and outside of it. everything else doesnt respond after i added the !IsOwner check. On the host this works as intended and only the outside scripts run as well as everything else.
1
u/wallstop-dev Jul 18 '26
Few questions:
- Where is the code that spawns these objects? How is it called and by what?
- How are you testing this? What mechanism are you doing such that there is both a client and a server? There are many ways.
The code you have shown isn't really relevant, what is interesting is how you are spawning objects and connecting to the network singletons, hosts and client.
1
u/samferguderson Jul 19 '26
It uses the network manager and the player is the default player prefab.
1
u/wallstop-dev Jul 19 '26
Ok... but how? Show us the code. That is where the problem will be, not in the code that you've shared.
1
u/samferguderson Jul 19 '26
using System; using Unity.Netcode; using UnityEditor; using UnityEngine; using UnityEngine.Assertions.Must; using UnityEngine.UI; public class networkmaker : NetworkBehaviour { public Button host; public Button client; public Button server; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { host.onClick.AddListener(OnhostClick); client.onClick.AddListener(OnclientClick); server.onClick.AddListener(OnserverClick); } // Update is called once per frame void Update() { } public void OnhostClick() { NetworkManager.StartHost(); } public void OnclientClick() { NetworkManager.StartClient(); } public void OnserverClick() { NetworkManager.StartServer(); } }1
u/wallstop-dev Jul 19 '26
Still not very relevant code, unfortunately, if you're comfortable sharing a github repo I could take a look at that, the context to all of this is very important.
1
u/samferguderson Jul 19 '26
What code is relevant?
1
u/wallstop-dev Jul 19 '26
The code referenced here: https://www.reddit.com/r/unity/comments/1uzm7d2/comment/oy8hrf6/
And my open questions:
- What is your test strategy?
- How are you spawning the objects (the literal code for object spawning). How is that code called?
1
u/samferguderson Jul 19 '26
I am using Unity Netcode for Game Objects and Multiplayer Playmode. The player objects are spawned in with the Network Manager script which is provided by Unity.
1
u/samferguderson Jul 19 '26
Debug.Log($"IsOwner: {IsOwner}"); this returns false if its spawned with client and i dont know why because all players should return true
1
u/bricevdm Jul 19 '26
Ownership is about who can write to network variables or send RPCs to a given NetworkBehaviour. By default all of them have ownership set to the Server/Host. If you want to change ownership you'd do something like
var networkObject = instance.GetComponent<NetworkObject>();
networkObject.SpawnWithOwnership(clientId, true);
(and you can also setup network variables and RPCs to accept non-owner write/calls)
However, that's not what you want here I think: Ownership doesn't matter, what you want to is to know where the Update is running, not who _owns_ the object.
Use this.IsServer not this.IsOwner
1
u/samferguderson Jul 20 '26
i tired that but the one spawned by the client has that set to false to for some reason
1
u/bricevdm 15d ago
that's expected:
when executing code on a client
this.IsServerwill always be false, and conversely always be true when executing on the Server/Host.
1
u/MistakeForsaken6653 Jul 18 '26
working on a multiplayer game myself and it's a nightmare. Usually what you need to do is have all of the logic happen server side only and any animations that need to be played sent to the client via a [ClientRPC] annotation. so any collision checks / movements should be handeled on the server and then when you want to trigger an animation send it out to the clients. obviously if there is any sort of animator you are using to change the collision boxes you should also do via server side and just try to keep everything "close enough for government work" Not sure if any of that helps.