cant understand this erro

okay so i got rather a long error, from what i understand it says some gameobject is null, but everything in my game runs just fine.

"
NullReferenceException: Object reference not set to an instance of an object
PlayerNetworkStuff.GetShot (Single damage, System.String enemyName, Boolean snowBlind) (at Assets/Script/Resources/PlayerNetworkStuff.cs:116)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[ ] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
NetworkingPeer.ExecuteRpc (ExitGames.Client.Photon.Hashtable rpcData, .PhotonPlayer sender) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2770)
NetworkingPeer.RPC (.PhotonView view, System.String methodName, PhotonTargets target, .PhotonPlayer player, Boolean encrypt, System.Object[ ] parameters) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3625)
PhotonNetwork.RPC (.PhotonView view, System.String methodName, PhotonTargets target, Boolean encrypt, System.Object[ ] parameters) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2893)
PhotonView.RPC (System.String methodName, PhotonTargets target, System.Object[ ] parameters) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:574)
Shooting.FixedUpdate () (at Assets/Script/Resources/Shooting.cs:58)
"

2869073–210190–PlayerNetworkStuff.cs (6.33 KB)
2869073–210191–Shooting.cs (2.31 KB)

What this code is supposed to do?

            HPSlider = GameObject.Find("NetworkManager").GetComponent<NetworkManager>().HPBar.GetComponentInChildren<Slider>();
            HPText = GameObject.Find("NetworkManager").GetComponent<NetworkManager>().HPBar.GetComponentInChildren<Text>();
            deathText = GameObject.Find("NetworkManager").GetComponent<NetworkManager>().deathText;
            blindImage = GameObject.Find("NetworkManager").GetComponent<NetworkManager>().snowBlind;

NetworkManager component is your custom class? There is just a built-in class with the same name that corresponds to Unity Networking. (I am not talking about Find function, but about GetComponent)

If it is your custom class, then I would recommend you to change name.

If you wanted to call this built-in class of networking, then there is no such variable as HPBar in this class. You probably want to get this from another place, but not from NetworkManager.

well the code gets the ui compnents

I understand what you want to get, but, obviously, the script doesn’t do what you want.

 HPSlider = GameObject.Find("NetworkManager").GetComponent<NetworkManager>()...

This code will get component/script of class NetworkManager on the object with tag “NetworkManager”. That is fine. But next part of the line:

...HPBar.GetComponentInChildren<Slider>();

Calls for HPBar variable in the class NetworkManager. The class doesn’t have such variable inside (It might be that another class on the object stores this variable). So, a question rises, where does this variable “HPBar” locate?

Again, if NetworkManager is a class that you have made, just tell me.

No i tjekked that, the script contains that value, and im also able to change all the components i get from there.
thats why the error is wierd, because all the objects work just fine

Just send here your NetworkManager script. Also, the first thing I would recommend you to do is to change name of it.

2869155–210207–NetworkManager.cs (5.86 KB)

Okay… So…

Firstly, when you use public field, don’t use [SerializeField] . It makes private fields visible in the inspector, but nothing to public.

Secondly, I will repeat, please, change name of the script to another, since unity already has built-in script with the same name.

changed the name removed serialized field, still gives me the error

At third,

HPBar.GetComponentInChildren<Slider>()

What is in the HPBar? I mean, are you sure that the component Slider should be found in children, but not on the object itself. I just dont know what exactly you put in the variable. Check please that the object you put in the variable is not the object with Slider component.

Yeah, probably it is correct, but check anyway.

Next step, I would like to ask you to debug the variable. After the block of code, that gets values from another script, type

Debug.Log(HPBar.value);

And look what it will show. It may show the value, the same error, but pointing at another line(it may still throw the same error, but only line will be different. so, pay attention to this), or it may show nothing. Debug will not work. It means that the problem is with photonView.

Sorry, I cannot tell you immediately where is the error, because I dont know how your project is constructed and maybe y. But I know what we should look at. So, sooner or later we will find the source of error.

the slider is in the child, and i have tried debugging that line, ir returns 100, which is the default number of my value in the slider component

You don’t have to change the name since Unity’s is in the UnityEngine.Networking namespace and since this appears to be using Photon I can’t imagine OP will be including it anywhere in their code.

That being said - line 116 in PlayerNetwork stuff. HPSlider is null which means your huge, compound find on line 60 failed (or the HPSlider was destroyed at some point). Break down line 60 into multiple lines to see if it succeeds or where it fails first off. If that is working (I’d still break it down anyway) then start working towards where it might have been destroyed.

To add to Kelso’s reply: either HPSlider was not found in line 60, OR possibly you’re calling “GetShot” on PlayerNetworkStuff before Start() is called. For example, if PlayerNetworkStuff is disabled in the hierarchy, Start() will never get called and HPSlider will still be null.

1 Like

Okay, so i split it up somehow and put in a lot of debug code to see where it breakes:

Debug.Log("Before getting stuff");
            nM = GameObject.Find("NetworkManager").GetComponent<NetworkManager>();
            Debug.Log("Got networkmanager Script: "+nM);
            HPSlider = nM.HPBar.GetComponentInChildren<Slider>();
            HPText = nM.HPBar.GetComponentInChildren<Text>();
            deathText = nM.deathText;
            blindImage = nM.snowBlind;
            Debug.Log("Got elements: "+HPSlider+";"+HPText+ ";" + deathText + ";" + blindImage);

Debug.Log("Sætter slider");
        HPSlider.value = health;
        Debug.Log("Sat slider til: "+HPSlider.value);
        HPText.text = health.ToString();
        Debug.Log(HPText);

Then when i run the code, the debug says every gameobject is returning, but it still gives me null error refrence at the “HPSlider”

Is it being destroyed at any point? As @makeshiftwings suggested - is the RPC being called before Start somehow?

I found the error, it was that the hpbar stuff was only set on the local player and not the players over the network, so when it tries to acces it it returned the error.

So short when i shot a player, that player tried to acces my players hp bar and failed