#Null Reference Issue

1 messages · Page 1 of 1 (latest)

light moon
#

Hello, guys! I have a small issue in my code. For one reason, it says that I have null reference on my best time text object but I am pretty sure it is assigned. Check my code here for more information and console:

GameManager.cs script

// Check for null bestTimeText
if (bestTimeText == null)
{
Debug.LogError("bestTimeText is null in Awake!");
}

else
{
LoadBestTimeOnStartGame();
UpdateBestTime(bestTime);
}

and

public void UpdateTimer()
{
if (timerText != null)
{
time += Time.deltaTime;
int minutes = Mathf.FloorToInt(time / 60);
int seconds = Mathf.FloorToInt(time % 60);
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
timerText.text = "Timer: " + timerText.text;
Debug.Log("Timer updates: " + timerText.text);
}
}

and

public void CheckSaveBestTime()
{
if (time > bestTime)
{
bestTime = (int)time;
PlayerPrefs.SetInt("BestTime", bestTime);
PlayerPrefs.Save();
Debug.Log("Best time is: " + bestTime);
Debug.Log("Checked and saved best score!");
UpdateBestTime(bestTime);
}
bestTimeText.text = bestTime.ToString();
}

and

public void UpdateBestTime(int bestTime)
{
if (bestTimeText != null)
{
bestTimeText.text = "Best Time: " + bestTime;
Debug.Log("Best time updates: " + bestTime);
}

else
{
    Debug.LogError("bestTimeText is null in UpdateBestTime!");
}

}

The errors occur when I press play button and enters the main scene of my game.

tacit burrow
#

lol mate you've been here long enough to know how to post code no ?

light moon
#

I know

#

I am long enough

#

I was just bored

#

No bored sorry

tacit burrow
#

its not the length its hard to read it in a discord message

light moon
#

I just had problem at specific lines of code

tacit burrow
#

yes but if your error in console says line 98 we need to see quickly what line 98 is for example

#

instead of asking you "which one is the line 98" etc

light moon
#

yes true my bad sorry

#

Wait I will send you now these lines of code

tacit burrow
light moon
#

Here is my game manager

#

Where I have assigned all of my text and things etc

tacit burrow
#

alr screenshot the console error you got

light moon
#

Here is the game manager script

#

Here is the console with the errors

tacit burrow
#

the lines dont matchup

#

oh nvm

#

you wrote Debug.LogError

light moon
#

yeah

#

So, it logs an error

tacit burrow
#

where bestTimeText was not assigned

light moon
#

I have this game object on my other scenes

#

as well

#

I have it as a prefab

#

But what I did was

tacit burrow
#

is bestTImeText part of the prefab?

light moon
#

I copy the best time object and attach it to my other scene in game manager

#

Tried to do that and some errors disappeared

#

before that I had 6 errors

#

now I have 3

tacit burrow
#

which ones

light moon
#

Check it herer

#

I have two different scenes here

#

one canvas for my other

#

and the second one for the other

#

I copy paste the best time and highscore

tacit burrow
#

why

light moon
#

now it is interesting

tacit burrow
#

no need to clone it

#

just make a canvas that is attached to game manager as DDOL prefab

light moon
#

I have done the same with highscore but there I dont have errors

#

I have only for my timer

#

Like you see

#

I don't like a lot that way I really want to make for each scene separate canvases

tacit burrow
#

its confusing where these objects are though

tacit burrow
light moon
#

If I want something to persist or to change across my scenes then i make it a prefab

tacit burrow
#

and use DDOL

light moon
tacit burrow
#

the canvas

#

for texts n UI

light moon
#

So to make it a child?

tacit burrow
#

you should not reference scene objects from a prefab

tacit burrow
light moon
#

So, to use my canvas inside the game manager?

#

To set canvases as child game objects of game manager parent game object?

#

That what you mean?

#

The point here is that I have these errors on my UpdateBestTime() method and on Awake() method at the lines of code it says in the console.

#

I was thinking if the problem is related with the types of the variables I have

#

Because for best score and score I am using integer

#

and for time and best time I am using different data type like a float to time and int to best time

tacit burrow
# light moon

none of int or floats would ever throw a nullr reference

#

unless i misunderstand what U meant

#

value types cannot be null (unless explicitly made so with ?)

light moon
#

Yeah I was just thinking

#

Because I have everything fine

#

Tried to contact chatGPT as well

#

He gave me information but didn't help

#

He said all of that

#

I was trying to get some useful information from it and use it I did but didn't help a lot

tacit burrow
#

spambot is useless

#

the issue Is because when you switch scenes the references get probably disconnected

#

the best way to do it is putting GameManager object as DDOL and make the InfoUICanvas as child so you can bring it with scenes with you

light moon
#

Can you show me an example of it you say?

#

So, chat told me to make game manager a singleton

#

Thats what you mean?

tacit burrow
#

is game manager not a singleton already ?

light moon
#

How to understand if it is or not?

tacit burrow
#

that probably means no

#

you didn't do public static GameManager Instance

light moon
#

I don't think it is singleton because I send to chat all of my code

#

and he said after that to make it singleton

#

so I probably dont have it

#

as singleton

tacit burrow
#

here is an example if you need to know how to make it

light moon
#

ok

tacit burrow
light moon
#

Oh, I have that saved you sent

#

Someone sent me a few weeks ago again and I forgot about that

#

Thanks that you remind me that!

tacit burrow
#

if you want to call the game manager method you can just do GameManager.Instance.Method()

light moon
#

So, I have to have my game manager script as static?

#

I mean in order to make it singleton

tacit burrow
#

no no not the script

#

the Instance

light moon
#

oh

#

ok

tacit burrow
#
public class Foo : MonoBehaviour
{
    public static Foo Instance { get; private set; }
#

Instance will be the Static

light moon
#

Hey! For that you said before I did something like that

#

Is it fine?

#

Now, I am going to do the other you said

tacit burrow
light moon
#

oh

tacit burrow
#

you would need to make GameManagerContainer the DDOl

light moon
#

So, can I do that?

#

Is it fine?

#

I want to make DDOl only game manager

#

So, i think I dont have to do that

tacit burrow
#

yes make gamemanager DDOl

#

but cannot be child of GameManager container

light moon
#

Hey, nav! Like I see it is not an error that I have to care about a lot because like I see I don't have any malfunctions at all.

#

I am still dealing with that errors but they are fine and do not make a threat to my game at all

#

What do you think?

#

You said before about Debug.LogError I will try to remove it

tacit burrow
#

if your statement is Debug.Logerror is firing that means your if condition is still null

light moon
#

It says that I have errors there but when I remove log error it says that I have not

#

Now I have tried to remove Debug.LogErrors and add debug.log

tacit burrow
#

because youre printing the error only if you dont have the component whch apparent you dont

light moon
#

It is not an error if it was then it should have been seen without the debug.logerror statement

#

What do you think?

tacit burrow
#

the error is telling you the Text object is null

light moon
#

It is not null

tacit burrow
#

if you remove the log error the error is still. there

light moon
#

I have everything at the correct place thats why

tacit burrow
#

send current updated code

light moon
tacit burrow
light moon
#

It prints now the same thing so the error persists like you said

tacit burrow
#

exactly

#

whenever you print a log do this

light moon
#

I am really disappointed now XD

tacit burrow
#

put Debug.Log($"{name} PlayerController not found!", gameObject);

#

then click the log when it prints

light moon
#

ok

tacit burrow
#

make sure there is only 1 game manager

#

if there is then your game manager doesnt have the field assigned during playmode

light moon
#

Yeah it is only one game manager when the main scene loads

light moon
#

I found the issue

tacit burrow
#

did ya?

#

right on time, i gotta go to work in 5 mins 😅

light moon
#

I haven't assigned there anything

#

Alright! I got the error and fixed it thanks for your help and have a good work 👋