About Health no emergencyyyyy(Laser Defender)

In LaserDefender,
As my health is going down an I wrote the player script like that if health ==0 destroy that player .And the player respond to my code all happy going but until the player health UI come when i am returning that health from player script to healthUI script, the player script returns all the value from player max health 500 to 100 but after 100 there is a null referance yeah i know where it is as the player has been destroyed but the healthUI script is still wanting the value frpm players script but there is no playerscript so null references coming .But i tried to solve it by writing is == null and !== null but nothing solved the problem.But it should solve the problem if i write ==null and !=null but i failed plzz solve it

Hi @satirthapaulshyam,

Welcome to our community! :slight_smile:

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Tnx for your speedy response .Yeah ,I got the reason why i am getting null reference .The reason is as the player script is returning value to my HealthUI script and the HealthUI script is showing the value in gamescene but when the health reaches to 0 we all know that the player should have been destroyed and we wrote code for it in players script and the player destroyed but when after the health reaches to 100 if we get a shot by enemy the players health reaches to 0(let us assume perenemydamage=100) and the player was destroyed according to player script but still the HealthUI is wanting to returnn the value from the players script but the player script was destroyed.But i create a solved for it but it didnt work

Actually, checking against null should have solved the problem. Could you share your relevant code (the HealthUI and the Player(?) or wherever the HealthUI gets accessed)?

See also:

tnx i’ve solved it by reimporting that code.Sometimes code wont work according to ur wish.
But i’ve one more question:
if you are loading a level in a function such that if u pressed a button that function will occur and take u to another level or scene and in that function if you also write findobjectoftype() after ,SceneManager.LoadScene(“GameScene”) Like the following function:
public void LoadGameScene()

    var ScorePrefab = FindObjectOfType<Score>();
    ScorePrefab.currentScore = 0;

But Any Score Type script is not present in your current scene (Let’s say your currentscene is mainmenu scene and your function is taking from mainmenu to gamescene)but Score type Script is in Gamescene
will the function work???

The function always works. The question is: Will it throw an error?

If the FindObjectOfType method cannot find a Score object, the next line will throw a NullReferenceException because the code attempts to access a non-existent object via the ScorePrefab variable.

yaaa . but at first iam moving the scene and in the next scene (GameScene)where i have moved to there is Score type script but it is throwing error But it should find that type script cause there exist Score type script.
Noted:there is no Score type Script in current scene .But I think it should not matter cause atfirst iam loading the GameScene i.e.:SceneManager.LoadScene(“GameScene”);
then the code execution is in GameScene .Now in GameScene I have Score type Script So it should find that Script bit it is throwing error

Did you apply the “singleton” pattern in one of your classes? If so, check if your “singleton” calls gameObject.SetActive(); in the same if-block where Destroy(gameObject); gets called. If there isn’t, add the line.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms