#null exception when spawning

1 messages · Page 1 of 1 (latest)

lime escarp
#

Which object is coming up as null?

empty pike
#

im not sure... The error shows up from a unity networking script when I press the button to spawn

#

the button itself has a network object component tho:

empty pike
#
Unity.Netcode.NetworkMetrics.GetObjectIdentifier (Unity.Netcode.NetworkObject networkObject) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Metrics/NetworkMetrics.cs:527)
Unity.Netcode.NetworkMetrics.TrackRpcSent (System.UInt64 receiverClientId, Unity.Netcode.NetworkObject networkObject, System.String rpcName, System.String networkBehaviourName, System.Int64 bytesCount) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Metrics/NetworkMetrics.cs:342)
Unity.Netcode.LocalSendRpcTarget.Send (Unity.Netcode.NetworkBehaviour behaviour, Unity.Netcode.RpcMessage& message, Unity.Netcode.NetworkDelivery delivery, Unity.Netcode.RpcParams rpcParams) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/RpcTargets/LocalSendRpcTarget.cs:52)
Unity.Netcode.ServerRpcTarget.Send (Unity.Netcode.NetworkBehaviour behaviour, Unity.Netcode.RpcMessage& message, Unity.Netcode.NetworkDelivery delivery, Unity.Netcode.RpcParams rpcParams) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Messaging/RpcTargets/ServerRpcTarget.cs:86)
Unity.Netcode.NetworkBehaviour.__endSendRpc (Unity.Netcode.FastBufferWriter& bufferWriter, System.UInt32 rpcMethodId, Unity.Netcode.RpcParams rpcParams, Unity.Netcode.RpcAttribute+RpcAttributeParams attributeParams, Unity.Netcode.SendTo defaultTarget, Unity.Netcode.RpcDelivery rpcDelivery) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs:399)
PlayerController.SpawnFireballRPC (System.Single x, System.Single y) (at Assets/PlayerController.cs:62)
PlayerController.Fireball () (at Assets/PlayerController.cs:56)
UnityEngine.Events.InvokableCall.Invoke () (at <44f3679c53d1477a9c6e72f269e3a3a9>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <44f3679c53d1477a9c6e72f269e3a3a9>:0)
UnityEngine.UI.Button.Press () (at ./Library/PackageCache/com.unity.ugui/Runtime/UGUI/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at ./Library/PackageCache/com.unity.ugui/Runtime/UGUI/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at ./Library/PackageCache/com.unity.ugui/Runtime/UGUI/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at ./Library/PackageCache/com.unity.ugui/Runtime/UGUI/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at ./Library/PackageCache/com.unity.ugui/Runtime/UGUI/EventSystem/EventSystem.cs:530)
#

did I set the RPC wrong ?

cloud knoll
#

is the fireballPrefab in the spawnable objects list?

empty pike
cloud knoll
#

Are you destroying anything on the player object after it's spawned?

empty pike
#

no

#

ohh hold up, I have a script from the docs that I copy pasted that makes it not server authorative...

using Unity.Netcode.Components;

[DisallowMultipleComponent] // Prevents you from adding the ClientNetworkTransform to the object more than once.
public class ClientNetworkTransform : NetworkTransform
{
    /// <summary>
    /// Used to determine who can write to this playerTransform. Owner client only.
    /// This imposes state to the server. This is putting trust on your clients. Make sure no security-sensitive features use this playerTransform.
    /// </summary>
    protected override bool OnIsServerAuthoritative()
    {
        return false;
    }
}
#

im changing the rpc to send to clientandhost instead

#

maybe thatll do something

cloud knoll
#

That won't work

empty pike
#

ur right lol

#

it shouldnt be sendtoserver anyway tho right? Since the topology is clienthosted, and I have that script from the unity docs to make it so im assuming. The host would act as the server, would send to server = send to host in this instance ?

cloud knoll
#

Host = Client + Server

#

There’s nothing wrong with your spawning code. You would get different errors for that

#

I’ve never seen the error you’re getting, there’s something else that you must be doing that’s off

lime escarp
empty pike
#

i was thinking it'd be weird to have every button be networked since that just seems inneficient. But I was following a code monkey tutorial and they mentioned with RPCs u want the object to have a network object component, the derived from NetworkBehaviour in the script, and the RPC header thing, I thought that first part he mentioned might have been something I needed to do for the button since its from the Event onclick

#

i didnt add it at first, it was just a troubleshooting thing i added afterward just in case

#

but its still not working :C

#

This is the playerprefab's inspector, is there something im missing from the network object and transform components ?

#

fireball prefab

#

ive tried the 2 different authoirty modes, owner and server but no luck

lime escarp
empty pike
#

through the prefabs player controller script, calls Fireball() which itself calls the fireballRPC with the given x, and y

#

and network object from the button removed 🫡

#

thats the structure code monkey used anyway in his example. Player's script calls a function that calls an RPC. Although his was an on mouse click event and mines a button click.

#

im assuming I cant directly put the RPC on the button click, but i doubt thats the issue anyway

#

alright I added some debugs, apparently the RPC is being called from something without ownership ?

public void Fireball()
{
    if (fireballPrebab == null)
    {
        Debug.LogError("Fireball prefab is not assigned!");
        return;
    }

    if (!IsOwner)
    {
        Debug.LogError("FireballRPC() called from a non-owner player!");
        return;
    }
    SpawnFireballRPC(finalPosition.x, finalPosition.y);
}```
lime escarp
# empty pike

How are you assigning this in the inspector when the player is not in the scene yet?

empty pike
#

... is this one of those "u cant do this with a prefab " things owo

#

i dragged the prefab of the player into it from the assets folder , I can't do that ?

#

ig this is where the GameManager comes into play

lime escarp
#

You need to assign the button listener in code when the local player spawns in

empty pike
#

she's alive !