null reference exception object reference not set to an instance of an object c#

um , i`m new to unity , and my c# program not that good , so basicaly i trying create new game and when i want my player get experience , this warning pop out … humm , so here the code :

void Die(){
dead = true;
DropLoot ();
foreach (GameObject go in players) {
go.GetComponent().GetExperience(expGranted/players.Length);
}
anim.SetInteger(“Condition”,4);
GameObject.Destroy(this.gameObject,3);
}
null warning pop out on go.GetComponent().GetExperience(expGranted/players.Length);

and here my get exp puppet script

public void GetExperience(float exp){
experience +=exp;
}

can anyone help me :) ? otherwise i will make rpg game without experience which so sad :)
thanks before :smile:

Either there are null GameObjects in players which I assume is a list or array. Or some of the players don’t have a puppet on them. Use Debug.Log(); on the GameObject and the puppet to figure out which one is null and go from there with figuring out why it’s null.

Please use code tags. There is a pinned thread in the forums on how to do so.

Did you get this fixed?

and how i do that :) ? im basically just 2 days on unity , barely know to use unity :`)

humm , https://forum.unity3d.com/threads/using-code-tags-properly.143875/ this one you means ?

Yes, that one :slight_smile:

foreach (GameObject go in players) {
if(go == null) {
   Debug.Log("go is null");
   continue;
   }
ShirorenzPuppet sp = go.GetComponent<ShirorenzsPuppet>();
if(sp == null) Debug.Log("sp is null");
else sp.GetExperience(expGranted/players.Length);
}

I think that’s what the other poster meant.

phew @methos5k thankss , i add if(go == this.gameObject) then it works (though now i got problem with animation :v , gotta create new thread :smile: )

:slight_smile: heh