null reference error that makes no sense

ok i am having a problem calling a function on another object, i have a game manage object and an enemy object, the scoring is setup on the game manager script, when the enemy gets killed it calls a function on the game manager script to add 20 points to the score, then in the same function it calls the function to display the new score, now the function runs properly if run from the game manager script but when the function is called from the enemy script it runs the function and adds the points as i have debug logged this line so i know the function is called but when it then calls the function to display the score it gives an object reference not set to an instance of an object error, but how can this be as it will run the same function from its own script, itis calling a function that has already run without an error, how can it say there is no reference to an object when it is in the same script.

it gives the error on this line:
ScoreText.text=“score:”+score.ToString();

but this line has already been called in the same script without error

any help appreciated

If you post a code sample of the GamaManager script as well as the EnemyObject, including the relevant code from both classes i’d gladly see if I can see anything but based on just your description alone i can’t offer any help.

Post code
Post the error including the line number it happens on

it seemd to be that the manager had to be a prefab so i could drag the text object prefab into its slot but now the score will not display in realtime and only updates when exit game., il try sort it, if i get deeper stuck i will post code

thanks

The problem now is i need to call a function on a gameobject script from a prefabs script but as you cant drag a non prefab object onto a prefab public slot, i have tried various solutions but just cannot get the link working , how do i in script from the prefab script access a script function on a game object.

please help

Prefabs are GameObjects. Are you trying to execute code on a prefab that isn’t in the scene?

You can’t assign scene objects to prefabs because there’s no guarantee the scene object exists when the prefab is used.

yeah i know i cant assign a game object to a prefab, that is the problem, i have a prefab that will be instantiated and the script on it needs to call afunction on a script on a game object in the scene, how do i link the game object script to the prefab so it can call the function.

The prefab needs to find the GameObject instance in the scene that it needs. There’s a variety of methods for doing this. Posting some code would help.