#singleton

1 messages · Page 1 of 1 (latest)

tacit spruce
uneven quartz
#

And did this script work?

tacit spruce
tacit spruce
uneven quartz
#

What specifically is the null reference on? You need to debug that line and verify what it is that's missing.

uneven quartz
#

The error gave you a line that the error occurred on. You have to figure out what on that line that code is causing the null reference.

#

I'm not going to hold your hand here like others are willing to do, so making random code changes and posing questions to me isn't going to work.

#

Start by actually getting the line the error occurred on.

tacit spruce
tacit spruce
uneven quartz
#

So what part of line 52 is the issue?

#

I count 4 things that could be null on that line

tacit spruce
uneven quartz
#

Start Debug.Log'ing each of the elements in it and see what returns null.

dusky oxide
#

See which of these is blank.

tacit spruce
dusky oxide
#

I'm missing the player2d check

#

Cache the player 2d in a variable and test that too

#
var p2d = p2.player2D;
Debug.Log...```
tacit spruce
#

alright,

#

also why p2?

dusky oxide
#

After the null error is thrown, check the logs and see which of these are printing ''

dusky oxide
#

Other than that, any of the above could be throwing null including the player2d variable.

dusky oxide
#

So in both, it thinks there's only one player.

#

Check the inspector for the manager

#

When are player one and two set up?

tacit spruce
dusky oxide
#

Well right now, all it's saying is that p2 is '' which implies that it's null

dusky oxide
#

You need to figure out why it's null by checking where you've assigned it - whether it was through the inspector with drag and drop or in some Start method with code etc

dusky oxide
tacit spruce
# dusky oxide

well.. that is because none of the players are in the level select scene

tacit spruce
# dusky oxide

like how can i do that? and even if i go in other levels?

uneven quartz
#

If your players are not in the level select screen, why do you need this object in your level select screen?

tacit spruce
uneven quartz
#

Then move them out of that script? Why is there a cameraadjust, playerbutton and transition inside a script for player management.

#

This is an architectural issue. Giving too many responsibilities to a single script that it's becoming unweidly.

uneven quartz
#

What are you showing me?

tacit spruce
uneven quartz
#

And?

tacit spruce
#

idk how i feel about moving them out of there because that owuld change a lot

uneven quartz
#

Yes, that's called refactoring and something you have to get used to.

#

You're at a dead end that you're hoping to fix with bandaids. It's just going to get worse.

#

But anyway, if you want to continue down this route, then add a check before you attempt to instantiate your players that those PlayerOne and PlayerTwo fields are not null.

tacit spruce
uneven quartz
#

Yes. If the issue is that those are empty and thus causing a null reference, first check that they're not null before you attempt to do the Instantiation.

tacit spruce
#

hmm... like.. != ?

uneven quartz
#

Research it, try it, learn how if statements work.

#

As I said, I'm not going to hold your hand if you refuse to learn the basics of programming (no, "trying" it by fumbling through making a game isn't learning)

#

But looking up how to check if something is null would be a start.

tacit spruce
#

i will try and look up

uneven quartz
#

This is also assuming that is the issue. I'm not completely following this codebase. It's large and messy. I'm assuming you've determined the null reference is a result of those fields being empty while in the level selection screen.

tacit spruce
#

yeah

tacit spruce
uneven quartz
#

These screenshots show nothing of what you're trying. They're logs.

tacit spruce
#

I know

tacit spruce
uneven quartz
#

So is your null reference happening in the level select screen or the gameplay scene?

tacit spruce
uneven quartz
#

What happens when you die when only testing the gameplay scene?

tacit spruce
#

I can respawn to the checkpoint (since I still have 3 lives as it's shown under the health bar)

uneven quartz
#

I'm specifically asking if the NRE occurs at any point during gameplay, if any when you're only testing the gameplay scene and not accessing it through the level select screne.

tacit spruce
uneven quartz
#

In your gameplay scene, do you have the playermanager there by default, with the player's assigned?

#

And in your level select screne, do you have the playermanager there by default, without the player's assigned?

tacit spruce
uneven quartz
#

That's not answering my entire question.

tacit spruce
uneven quartz
#

Are your player objects prefabs?

#

Or are they just in your gameplay scene that you've dragged in from the scene?

tacit spruce
uneven quartz
#

But which did you drag into your playermanager script?

  1. The prefabs themselves from your project folder
  2. The prefab instances that exist in your scene
tacit spruce
uneven quartz
#

So here's your issue from what I understand.

  1. You have a singleton for PlayerManager. It's a DDOL object so that whatever scene you load first, it will use that object throughout your game. This means that when you load your Level Select scene, it will use that PlayerManager throughout.

  2. You have not assigned any players to your PlayerManager in your Level Select so that when this PlayerManager is used in your Gameplay Scene, it throws an NRE

#

What you should do, is assign the prefabs of the players to your PlayerManager script so that it instantiates those. Not the ones currently in your scene.

tacit spruce
tacit spruce
uneven quartz
#

I'm telling you to drag the actual prefabs themselves from your project folder.

trim berry
#

Or, if you followed the instructions yesterday and already have them in different variables, use the SpawnPlayer method to create new instances when you change scenes

tacit spruce
uneven quartz
#

You shouldn't have both the player prefab and the player itself. Just have the player prefab, instantiate it, and use that object at runtime.

trim berry
#

The issue with all this is that helping with any code takes so long people just give up and leave, and then the next person has to come in and find out how to work with the half-implemented systems the previous person abandoned with no idea of where they intended to go if it didn't take seven hours to explain how to call a method

uneven quartz
#

Yeah, absolutely

tacit spruce
trim berry
#

Is that being used when you change scenes

tacit spruce
trim berry
tacit spruce
trim berry
tacit spruce
trim berry
# tacit spruce but what do i do then? (like how I can have them spawned in?

Assuming the players exist in the new scene and you intend to re-acquire instead of spawning them in at runtime:

Step 1: Find a way to call a function when a new scene is loaded
Step 2: Get the references to the player (Go ahead, use find, it's easier than explaining how to do this another way)
Step 3: Update the playerOne and playerTwo variables to those objects

#

Consider step 1 to be your assignment -- learning how to find your own solutions by searching

#

I'm not posting the code for this

tacit spruce
#

and step 2. "FindGameObjectWithTag"?

trim berry
tacit spruce
trim berry
#

Then why are you asking about step 2

tacit spruce
trim berry
#

I gave them numbers for a reason

tacit spruce
trim berry
#

explain your thought processes to how that solves step 1

tacit spruce
#

hmmm... something which... cannot be instantiated.... can't create an object of a static class and cannot acess static members

tacit spruce
trim berry
#

As long as it solves step 1, it works

#

So find something that calls a function when a new scene is loaded

tacit spruce
trim berry
tacit spruce
tacit spruce
#

calls a function when a new scene is loaded.

trim berry
#

Okay, and do you see anything from the unity manual in your search results

#

Part of learning how to search is learning how to find verifiable, current results. The Unity Manual is a good source to look for

#

Forum posts and stack overflow questions are just other users, and could be incorrect or out of date

tacit spruce
trim berry
trim berry
#

Look at your google results. Look for the page it's on

tacit spruce
trim berry
#

Did you scroll

#

there's more than one result

tacit spruce
trim berry
#

Well, considering I've literally just posted a screenshot of it, I'm going to go ahead and say "false"

tacit spruce
#

the fact that i can't find SceneManager.sceneLoaded either

#

neither can i find that in unitymanual

#

the only things i find about call a functino when scene is loaded is questions

tacit spruce
#

I just. don't get why

#

this is getting nowhere...😢

#

but.... I don't think this works because i cannot make 2 player to appear..

trim berry
#

Why and what does this have to do with step 1

tacit spruce
#

I just can't find one..

#

I don't know which method i should use

tacit spruce
#

i don't know what is the answer for step 1...

tacit spruce
#

yet idk how that would help me...

tacit spruce
tacit spruce
trim berry
#

You can now call a function whenever the scene changes.

#

Now you can move on to step 2 in that function

tacit spruce
# tacit spruce well?

ok, right it's supposed to be in the playermanager script? i just want to make sure

trim berry
#

Considering that's the one that will actually persist between scenes that is a likely place to put it

tacit spruce
tacit spruce
trim berry
#

Now step 3

tacit spruce
# trim berry Now step 3

hang on, am i suppoed to but the
GameObject.FindGameObjectWithTag("Player")
GameObject.FindGameObjectWithTag("Player2") somewhere in the PlayerManager?

tacit spruce
#

yeah

trim berry
#

why do you think I asked you to find a way to call a function when the scene loads

tacit spruce
#

i guess second image

trim berry
mighty scroll
median laurelBOT
mighty scroll
#
// your code goes here
tacit spruce
# trim berry Now step 3

so about Update the playerOne and playerTwo variables to those objects... do you mean by adding the tag names there?

trim berry
#

I mean Update the playerOne and playerTwo variables to those objects

#

I am not going to explain how to set a variable

mighty scroll
#

Which doesn't mean adding tags to them, as it will also throw an error if they're null

tacit spruce
trim berry
mighty scroll
trim berry
mighty scroll
tacit spruce
mighty scroll
mighty scroll
#

How are you going to implement this using a method?

tacit spruce
mighty scroll
#

Which means they should be assigned via the method

trim berry
#

You've got the method. You've got the data. Literally just assign the variable

#

this should not be something that requires further explanation

#

If it is, then you need to stop literally everything you are doing and not come back until you've done at least one course on !learn

median laurelBOT
#

:teacher: Unity Learn ↗

Over 750 hours of free live and on-demand learning content for all levels of experience!

trim berry
#

Assigning a variable is not complicated. It is not jargon. It is not some esoteric voodoo space magic. It's literally the single most fundamental concept in any form of programming in every language on the planet

trim berry
tacit spruce
trim berry
mighty scroll
trim berry
#

and when the scene changes

#

you know

tacit spruce
trim berry
#

the entire fucking point of what we've been doing for four goddamn hours

#

assigning them when the scene changes

#

what do you think we've been doing all of this for

#

please for the love of fuck retain a thought for longer than ten seconds

tacit spruce
# tacit spruce level select scene

how can i assign the two player there while none of th eplayers are in the level select scene? and even though if i use player prebas instread, it wouldn't work

trim berry
trim berry
#

No the goddamn fuck they are not

mighty scroll
tacit spruce
trim berry
mighty scroll
# tacit spruce yes they are!

Let's say you have a girlfriend in the city A. When you move to the city B, even though your girlfriend is assigned in the city A, you now don't have a girlfriend.
You have to implement a method, which automatically finds you a girlfriend when you move to another city.
Does it make it clearer?

tacit spruce
trim berry
#

You have the data. You have the function that runs when the scene changes.

#

So just

#

Assign

#

The

#

Fucking

#

Variables

tacit spruce
trim berry
#

this is nonsense

#

this isn't what we're doing

#

pay the fuck attention

#

jesus christ do you even know why we are here

#

it's been four hours today

#

seven yesterday

#

what am I doing here

#

I could have been a doctor

mighty scroll
#

Oh, yesterday too?

mighty scroll
# tacit spruce

Alright, could you answer the question:

  • What happens to your girlfriend, assigned in the city A, when you move to the city B?
tacit spruce
#

stays in city A?

mighty scroll
trim berry
mighty scroll
#

I have almost explained it

trim berry
#

No, you haven't. You think you might have

#

but you have not

tacit spruce
#

I know I have the data, but how i can assign the data?

mighty scroll
#

They just gotta pay attention to my questions

mighty scroll
trim berry
tacit spruce
mighty scroll
tacit spruce
#

goes to City B?

mighty scroll
#

Alright, let's do it another way

#

Tell me in case this example may hurt

#

Let's say you have lots of friends in your hometown, city A

#

Then you move to the another town, city B

#

And you don't have any contact with your previous friends

#

What does your List<GameObject> friends variable equal to?

#

Think logically

#

And faster, please

#

Cause I don't really have much time

tacit spruce
#

....I don't know...

mighty scroll
#

What?

#

Alright

#

Based on what I have written

#

Do you have friends?

tacit spruce
#

the friends are in city A and I am in city B

mighty scroll
#

Right

#

And you have lost all the contact with them

#

Do you have friends?

tacit spruce
#

and that would like no friends in City B

mighty scroll
#

owuld?

#

Could you rephrase the sentence?

tacit spruce
#

no contact with friends while in city B will mean no friends in city B

mighty scroll
#

Yes

#

So do you have friends?

tacit spruce
#

no

mighty scroll
#

Let's make a game out of it

#

The city A is the scene called City A

#

The city B is the scene called City B

#

You have a script called LinedolController

#

This script has

public List<GameObject> friends;
#

In the City A, this List is assigned to Bob, Alisa, Ella, Emma, Olivia, Manuela and Jon

#

Now you have a plenty of friends in the City A!

#

But, suddently, you have to move to the City B

#

How many friends do you have? What does the list friends equal to?

#

I feel like there ain't nothin to think bout, right?

tacit spruce
#

city A friends equeal to 7. moving to city b. city B friends equal to 0

mighty scroll
#

Right!

#

So does your friends list become empty?

tacit spruce
#

yeah

mighty scroll
#

Now let's look at your issue. You have a scene with the variables playerOne and playerTwo.

#

Imagine those are your friends

#

What happens to them when they are assigned in the initial scene, but then you switch to the another scene?

tacit spruce
#

.... they are no longer there

mighty scroll
#

Right. So tell me, what was digiholic trying to tell you?

tacit spruce
#

but this is like the opposite around, you start at the level select scene. and the players are in the gameplay scene

tacit spruce
mighty scroll
#

Right!

#

You edited it

#

There's the SceneManager.sceneLoaded event, which is called when...?

tacit spruce
#

when the players are seen in the next scene?

mighty scroll
#

Read carefully

#

Let's say, it's an event LinedolLifeManager.whenCityIsChanged

tacit spruce
#

yeah

mighty scroll
#

So when is it called?

tacit spruce
#

when you switched to next scene

mighty scroll
#

Yes

#

Which happens every time you call ...your answer goes here... method

tacit spruce
#

yeah...

mighty scroll
#

"yeah" is not the correct answer

tacit spruce
#

in the next scene, no friends are there

mighty scroll
#

So when is the scene changed?

#

Are your scenes changed when I want?

tacit spruce
#

when you move from city A to city B

mighty scroll
#

Right, now in your game

#

Who moves you from the cities?

#

Are you teleported to the city B randomly?

tacit spruce
#

no

#

I move from City A to City B once you select to go to the next scene

#

but yet. i am still confused on how can i assign the data or anything in the script like i don't have anything to assign there

mighty scroll
#

Let's say, in the example with the friends, I may find a friend by searching for a 2 meter tall dude, who shares the same interests

tacit spruce
tacit spruce
#

llike this

tacit spruce
#

I know I naswered the questions which is obvious. and that they are assigned in the gameplay scene, but I can't assign them in level select scene

#

I know I have the data, but I can't assign it anywhere in the level-select scene

tacit spruce
#

I have the player tag names there which is assigned to the function.

#

do you mean assign them in the code?

delicate solar
#

yes

#

GameObject.FindGameObjectWithTag("Player"); this line here is missing 1 thing to make it magically work

tacit spruce
delicate solar
#

that's not the missing thing

tacit spruce
delicate solar
#

mhm

tacit spruce
delicate solar
#

well the second two are definitely wrong

#

and mb, it's missing two things. I forgot you were looking for the component

#

so you also need something in there to get the component

tacit spruce
delicate solar
#

you're nearly there yeah

#

just need to use correct syntax

tacit spruce
delicate solar
#

ok so now fix it and make it variable assignment (in code)

delicate solar
#

woooo

tacit spruce
#

now to see if it works

#

of course i sholudn't forget player 2

mighty scroll
delicate solar
#

at least this way visibly guarantees the tags are in the right order...

mighty scroll
delicate solar
#

I agree

mighty scroll
#

Which shouldn't be used by them

delicate solar
#

you're welcome to correct the mistake yourself

mighty scroll
#

There is no need to find the player by its tag if it already has the PlayerController script, which I have then mentioned below

mighty scroll
mighty scroll
#

And the T should be set to PlayerController

#

As you, hopefully, have a single PlayerController in your scene

mighty scroll
#

I would recommend finding the objects of type PlayerController using FindObjectsOfType first

#

Then assigning the desired players

#

And I hope the method it's done in is subscribed to the sceneLoaded event

tacit spruce
tacit spruce
dusky oxide
dusky oxide
# tacit spruce

Having the variables be local only to that method looks incorrect though 🤔

#

The more appropriate solution would probably be to have a script on the player objects that would access the player manager and assign themselves as the player targets - both player one and two who are already in the scene would access the player manager in Start and assign themselves as the valid players.

#

Example:cs //You'd reference the player component on this same object from the inspector public PlayerComponents playerCom;``````cs //In the Start method: PlayerManager.instance.SetPlayer(1, playerCom);

#

Where you'd want to define some method in PlayerManager:cs public void SetPlayer(int playerNum, PlayerComponents playerCom) { switch(playerNum) { case 1: playerOne = playerCom; break; case 2: playerTwo = playerCom; break; default: Console.WriteLine($"Invalid player index: {playerNum}"); break; } }

dusky oxide
#

These would not be on/in the PlayerManager script

tacit spruce
#

instead it should be in the player script?

#

all of it?

dusky oxide
#

Can you show me the player component script?

dusky oxide
tacit spruce
dusky oxide
#

It's already the PlayerComponents component so you won't need to reference the component (itself) from the inspector

#

You'll want to assign the player number from the inspector though

tacit spruce
dusky oxide
#

Most likely 🤷‍♂️

tacit spruce
dusky oxide
#

This should have the players register themselves as players in the scene when the scene loads

#

You'll still get a null reference exception error if you've only got one player but your code expects two though.

#

You'll either have to do with the guard statements (if(player x not null) ...) or have an event base system (a bit more complex)

#

Or decouple logic belonging to the players from the player manager and let the players be responsible for them. I'm not certain if this is possible though as I'm likely one of the many people who have helped you and do not know the intentions of everything (yes, I've seen the code but do not bother to read and mainly skim over the stuff I had not introduced)

tacit spruce
dusky oxide
#

Well, what's the current issue?

dusky oxide
#

I don't think you need the method from the second image, at all

#

It shouldn't be the managers job to find the player with the design I intended above but rather the players who tell the manager if they're available.

tacit spruce
dusky oxide
#

If they're in the scene, they'll call their Start method after all Awake methods have been called - once.

#

Start is called once by any and every mono behaviour object

tacit spruce
dusky oxide
#

As long as you can assure that they're in the scene they'll assign themselves during Start

tacit spruce
dusky oxide
#

It doesn't matter

#

If they are in the scene loaded they'll assign themselves

tacit spruce
dusky oxide
#

||I don't fix code that isn't an issue or doesn't have a question to go along with it||

tacit spruce
#

also, what do i do next?

dusky oxide
dusky oxide
#

I can read them but I won't make an assumption or any suggestions - relative to what you're trying to do, the task given to you or it's purpose.

tacit spruce
# tacit spruce

i playtested a bit, and it seems to be working, but let's see what the others think about it

dusky oxide
# tacit spruce

You probably do not need those lines anymore tbh
They would be necessary if you're really trying to have your player manager find every player when a scene change rather than letting the players tell the manager they're available.

tacit spruce
#

also while I was taking a break from it, i made some tiles for the world Sweetland for the full one (

dusky oxide
mighty scroll
# tacit spruce

Better to do by finding the objects of type PlyaerComponent and then getting the tags from them. Also your method only assigns the 2nd player

#
PlayerComponent[] players = FindObjectsOfType<PlayerComponent>();

playerOne = players.FirstOrDefault(p => CompareTag("Player 1"));
playerTwo = players.FirstOrDefault(p => CompareTag("Player 2"));
#

You can also make a method for accessing a player

mighty scroll
tacit spruce
#

Ok

tacit spruce
#

this

tacit spruce