com.unity.netcode.gameobjects
com.unity.netcode.gameobjects copied to clipboard
Help with netcode bullet prefab spawning
Afternoon, so I have been struggling with this question quite a bit now (spawning bullet prefabs and syncing tire materials,trails,values over the network) , but the way I have been trying to do it is not as simple as just spawning the bullets and synchronising them how do I explain this. I have a bullet or rather a projectile blue print and 3 list of projectileconfig scriptable objects (which determine the projectiles data such as the damage,speed,sprite, etc.t.c) now everything works perfectly on the host and is even syncing to other clients as well but for some reason on the client when ever the bullet is spawned it immediately gets despawned and then gives me an error something amongst the line of only server can despawn networkobjects, yes I used to call a despawn function on the projectiles when they collide with something I have tried to make it a server rpc or make it so only the server calls it I even removed the whole despwing at one point but alas the issue still persist I will provide some of the scripts that work with the projectile script an if anyone can help point out what is wrong with the scripts whould be much appreciated cause I have tried so many approaches to solving this but for the last few weeks my efforts have been In vain. Down below are the scripts I use and note I am using netcode version 1.1.0 and I cannot upgrade any higher then that cause I am limited by the tech I use.
### Tasks
PROJECTILScripts.zip Here are the scripts, the general weapon script handles the spawning of the projectile blue print and initializing its config scriptable object the qued networkobject pool from multiplayer contribution gets pools and ques the projectile blue print and the general projectile script handles the projectils behaviour everything else is just extra
bullet is spawned it immediately gets despawned and then gives me an error something amongst the line of only server can despawn networkobjects
Could you provide the stack trace for this? It would help to know what is invoking the despawn method.
Sorry for the late response I was busy with exams, I have actually changed the scripts a little bit and the despawn error is no longer showing for now but I'm pretty sure it will pop-up again the current issue is that the projectiles are being spawned on the client side too but the initialization initialization of the clients projectiles dosent happen on either the client nor the host I will be sending the updated scripts soon
Here are the 2 main errors that occur when ever I spawn a bullet on the clients side NotServerException: Destroy a spawned NetworkObject on a non-host client is not valid. Call Destroy or Despawn on the server/host instead. Unity.Netcode.NetworkObject.OnDestroy () (at Library/PackageCache/[email protected]/Runtime/Core/NetworkObject.cs:451) UnityEngine.DebugLogHandler:LogException(Exception, Object) LobbyRelaySample.LogHandler:LogException(Exception, Object) (at Assets/AllScenes/com.unity.services.samples-3.game-lobby-main/Assets/Scripts/GameLobby/Infrastructure/LogHandler.cs:60) UnityEngine.Debug:CallOverridenDebugHandler(Exception, Object)
2 [Netcode] Behaviour index was out of bounds. Did you mess up the order of your NetworkBehaviours? #0 GetStacktrace(int) #1 DebugStringToFile(DebugStringToFileData const&) #2 DebugLogHandler_CUSTOM_Internal_Log(LogType, LogOption, ScriptingBackendNativeStringPtrOpaque*, ScriptingBackendNativeObjectPtrOpaque*) #3 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object) Updated Scripts.zip
And also here are the updated scripts ,
@NoelStephensUnity The projectile only spawns on the client side for a very little amount of time then it just gets destroyed and on the server side when the client spawns a bullet it dosent seem to move note I am using a network transform and rigidbody on the projectiles, but even as it dosent seem to move it sill does damage to the host over a certine distance away from the client
@Mid-D-Man Will continue to look over them, but the very first thing that stands out is this:
public async override void OnNetworkSpawn()
{
currentBeforeDespawn = 0;
await GetProjectileInfo();
Debug.Log("Bullet spawned");
}
I wouldn't recommend using Tasks in your MonoBehabiours (you should use coroutines), but I am pretty sure your projectile code is not doing what you want...and could be blocking the spawn process...if you want your task to "fall through" until some awaitable condition is met then you would want to do:
public async override void OnNetworkSpawn()
{
currentBeforeDespawn = 0;
var projectileGetterTask = GetProjectileInfo();
projectileGetterTask .ContinueWith(() => { base.OnNetworkSpawn();});
}
However, you really should avoid using Task and use a coroutine... of course I don't think the GetProjectileInfo method is actually doing any form of waiting...
You might take a look at this code as it might help as a guide to spawning bullets: BulletVisualSynch.zip
It doesn't have the unique configurations, but you should be able to apply configuration information to NetworkVariables and upon spawning (depending upon whether you are going with a server authoritative or owner authoritative model) the non-owners/client instances should already have their NetworkVariables set by the time the OnNetworkSpawn method is invoked.
But I would highly recommend not making any of the existing NGO SDK classes' defined virtual methods asynchronous as that will lead to issues for sure.
Ok I'll go see I I can make it work thanks alot