#singleton
1 messages · Page 1 of 1 (latest)
And did this script work?
yeah, when i was in the levle scene, but now when i was in a level select scene and select a level
the first one is from when i tested in a level scene while in the second one is what i playtested in a level select scene and select a scene to go to
i just. don't get it... even though it also have DontDestroyOnLoad...
What specifically is the null reference on? You need to debug that line and verify what it is that's missing.
liek this?
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.
well it was on line 52 "PlayerComponents go = PlayerManager.instance.SpawnPlayer(1, PlayerManager.instance.GetPlayer(1).player2D.lastCheckpoint.transform.position);"
checkpoint.SpawnNewPlayer (system.boolean. player1) in checkpoint.cs:52 which it says in the nullreferenceexception
So what part of line 52 is the issue?
I count 4 things that could be null on that line
to be honest, I don't know what part of line 52 is the issue
PlayerComponents go = PlayerManager.instance.SpawnPlayer(2, PlayerManager.instance.GetPlayer(2).player2D.lastCheckpoint.transform.position);
Start Debug.Log'ing each of the elements in it and see what returns null.
var pmi = PlayerManager.instance;
Debug.Log($"Player manager instance is: '{pmi}'");
var p2 = pmi.GetPlayer(2);
Debug.Log($"Player 2 is: '{p2}'");
var lcp = p2.lastCheckPoint;
Debug.Log($"Last check point was: '{lcp}'");```
See which of these is blank.
it just a red underline under lastCheckpoint;
I'm missing the player2d check
Cache the player 2d in a variable and test that too
var p2d = p2.player2D;
Debug.Log...```
After the null error is thrown, check the logs and see which of these are printing ''
If there's only one player, trying to get and use player two from the manager will throw an error.
Other than that, any of the above could be throwing null including the player2d variable.
first one is single player and second one is multiplayer
So in both, it thinks there's only one player.
Check the inspector for the manager
When are player one and two set up?
actually. when i played as single player before, happened the same thing to the single player
Well right now, all it's saying is that p2 is '' which implies that it's null
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
idk, something to do with player manager?
well.. that is because none of the players are in the level select scene
like how can i do that? and even if i go in other levels?
If your players are not in the level select screen, why do you need this object in your level select screen?
or else the cameraadjust, playerbutton and transition won't work if i remove it from the level select scene
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.
What are you showing me?
the playermanager code
And?
idk how i feel about moving them out of there because that owuld change a lot
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.
https://gdl.space/fezezovuzu.cs like separate that?
add a check?
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.
hmm... like.. != ?
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.
i will try and look up
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.
yeah
that is what i'm trying but i don't know how (the first image is when I'm in the level scene the second scene is when I'm in the level select scene and went in the level scene)
These screenshots show nothing of what you're trying. They're logs.
I know
and that determine, when you press on the level select button and when you went over to the scene to see if lpaer 1 (or 2 players) are there in the scene
So is your null reference happening in the level select screen or the gameplay scene?
when i playtested in the gameplay scene, there's no nullreference, but when i'm in level select screen and click on the button to go to the gameplay scene. and when i die, that's when null reference appear
What happens when you die when only testing the gameplay scene?
I can respawn to the checkpoint (since I still have 3 lives as it's shown under the health bar)
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.
in the playermanager script, in the first image when i'm in the gameplay scene. the player's object is in the script. while in the second image, in level select and when i select a level, the player's objec tin the script is not there
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?
the playerManager object in the layer is default
That's not answering my entire question.
i have both of the playermanagers there by default. while the gameplay scene have the player's assigned and the level select screen not assigned the player
Are your player objects prefabs?
Or are they just in your gameplay scene that you've dragged in from the scene?
the player objects in the gameplay scene are prefabs because of the blue color on the text
But which did you drag into your playermanager script?
- The prefabs themselves from your project folder
- The prefab instances that exist in your scene
in the playermanager script in the gameplay scene, i dragged from the objects in the scene
so 2
So here's your issue from what I understand.
-
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.
-
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.
yeah, it do have the player prefabs, two above
they have player prefabs in the playermangaer script, which is shown in the playermanager script
You specified here that you dragged the ones from your scene. Those are not the player prefabs themselves, those are the instances: #1237752727564582952 message
I'm telling you to drag the actual prefabs themselves from your project folder.
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
i meant i dragged the player objects from the scene in the lpayer 1 and 2 methods while i dragged the player prefabs in player 1 prefab and player 2 prefab in the script
I have the SpawnPlayer there
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.
I had them keep both public so they can be accessed from other scripts. The SpawnPlayer method sets these variables from the prefabs.
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
Yeah, absolutely
PlayerComponents go = PlayerManager.instance.SpawnPlayer(1, PlayerManager.instance.GetPlayer(1).player2D.lastCheckpoint.transform.position);
SpawnPlayer is right there
Okay and where is that
Is that being used when you change scenes
it's in the checkpoint script
Okay and are we dealing with checkpoint respawning right now
it works with respawning when I was in the gameplay scene, but when i was in the level select scene and select the gameplay scene and when i die, I no longer respawn and get a nullreference
Whenever you change scenes you will need to re-set your player references. This is why I suggested not having the players dragged in but spawned at runtime. This is the tradeoff I mentioned. Now you have to reacquire when you change scenes
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
and step 2. "FindGameObjectWithTag"?
are you done with step 1 yet
not yet
Then why are you asking about step 2
i was just asking
I gave them numbers for a reason
- to use "static"?
explain your thought processes to how that solves step 1
hmmm... something which... cannot be instantiated.... can't create an object of a static class and cannot acess static members
there's so many different methods from what i seacrhed up and it's hard to know which would solve my problem
Because there's probably many different methods to solve the problem.
As long as it solves step 1, it works
So find something that calls a function when a new scene is loaded
it's so hard to find which is working because how many different methods there is and that i don't know which is the right one because all is just about nextscene and stuff
Because there's many different methods to solve the problem.
As long as it solves step 1, it works.
So find something that calls a function when a new scene is loaded.
i cannnot find anything that something calls a function when a new scene is loaded, the only things i found on my search is loading a new scene, nothing else
What are you searching
calls a function when a new scene is loaded.
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
https://forum.unity.com/threads/how-do-i-call-a-method-after-loading-a-new-scene.613363/ someone also have the problem but now much answers there
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
Look at your google results. Look for the page it's on
i searched it up on google results but can't find it with unity manual
yes i did, yet i can't find which unity manual that holds holds that call a function when a new scene is loaded
Well, considering I've literally just posted a screenshot of it, I'm going to go ahead and say "false"
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
so i can't find that same thing anywhere in unitymanual
I just. don't get why
this is getting nowhere...😢
i did something...
but.... I don't think this works because i cannot make 2 player to appear..
Why and what does this have to do with step 1
I just keep on going around in the searches and found something....
I just can't find one..
I don't know which method i should use
AND I can't find this in the google reasults
i don't know what is the answer for step 1...
that like OnEnabled() used to add OnSceneToLoad?
yet idk how that would help me...
... are you there?
well?
Congratulations. You've completed step 1
You can now call a function whenever the scene changes.
Now you can move on to step 2 in that function
ok, right it's supposed to be in the playermanager script? i just want to make sure
Considering that's the one that will actually persist between scenes that is a likely place to put it
ok, while there's LevelLoader script that function on which you can enter a scene like when you press a button. while PlayerManager is the script that would help between the scenes like some objects and responsible for the screensize
GameObject.FindGameObjectWithTag("Player")
GameObject.FindGameObjectWithTag("Player2")
Now step 3
hang on, am i suppoed to but the
GameObject.FindGameObjectWithTag("Player")
GameObject.FindGameObjectWithTag("Player2") somewhere in the PlayerManager?
Do you remember step one
yeah
why do you think I asked you to find a way to call a function when the scene loads
i guess second image
Now step 3
!code
📃 Large Code Blocks
Use links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/, https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
// your code goes here
so about Update the playerOne and playerTwo variables to those objects... do you mean by adding the tag names there?
I mean Update the playerOne and playerTwo variables to those objects
I am not going to explain how to set a variable
The playerOne and playerTwo variables have to be assigned
Which doesn't mean adding tags to them, as it will also throw an error if they're null
oh
do you mean assigning them in the level select scene?
They mean step 3
Well, I haven't followed the conversation fully. Guess, you're supposed to assign them when the scene is loaded?
The steps, for reference to anyone else who dares tread here ^
Lol, this was already 3 hours ago
that is what i meant
How are you going to assign the variables in the Inspector when calling a method?
How are you going to implement this using a method?
I don't know, the assigned players are already there
Well, they told you to assign them when the scene is loaded
Which means they should be assigned via the method
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
:teacher: Unity Learn ↗
Over 750 hours of free live and on-demand learning content for all levels of experience!
they are assigned
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
Show
gameplay scene
Okay
Are they going to be assigned when the new scene is loaded?
level select scene
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
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
You have the data. You literally just need to assign the variables to the objects you just found.
THEY ARE ASSIGNED!
No the goddamn fuck they are not
Read the previous messages
yes they are!
Do you even look at your own goddamn messages. I know you ignore mine but do you even read your own
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?
I REMOVED THEM AFTER! because it doesn't work! this is for the level select scene. shoudl I use the prefabs in these?
You have the data. You have the function that runs when the scene changes.
So just
Assign
The
Fucking
Variables
This is not relevant
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
Oh, yesterday too?
Alright, could you answer the question:
- What happens to your girlfriend, assigned in the city
A, when you move to the cityB?
stays in city A?
What happens with your current GameObject girlfriend variable?
Metaphor doesn't work.
Code doesn't work.
Examples don't work.
Explanations don't work.
Translation doesn't work.
I just
I just don't think there is a way to explain this to them
It's possible with girlfriends.
I have almost explained it
I know I have the data, but how i can assign the data?
They just gotta pay attention to my questions
Via method
Have fun
...stays in city A?
Goddamn
goes to City B?
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
....I don't know...
the friends are in city A and I am in city B
and that would like no friends in City B
no contact with friends while in city B will mean no friends in city B
no
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?
city A friends equeal to 7. moving to city b. city B friends equal to 0
yeah
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?
.... they are no longer there
Right. So tell me, what was digiholic trying to tell you?
but this is like the opposite around, you start at the level select scene. and the players are in the gameplay scene
to assign them in the gameplay scene?
Haven't you just said that it's the opposite way around?
Right!
You edited it
There's the SceneManager.sceneLoaded event, which is called when...?
when the players are seen in the next scene?
yeah
So when is it called?
when you switched to next scene
yeah...
"yeah" is not the correct answer
in the next scene, no friends are there
when you move from city A to city B
Right, now in your game
Who moves you from the cities?
Are you teleported to the city B randomly?
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
How can you find the playerOne and playerTwo variables in any scene?
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
the players are ine the Gameplay scene, but they are called Player 1 and Player 2
they are in the gameplayscene, Player 1 and Player 2. and they are assigned in the gameplay scene
llike this
are you there?
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
I have the player tag names there which is assigned to the function.
do you mean assign them in the code?
yes
GameObject.FindGameObjectWithTag("Player"); this line here is missing 1 thing to make it magically work
player 1's tag name is just "Player"
that's not the missing thing
... assigning.. the playerOne and playerTwo right next to them?
mhm
in what way?
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
almost like that?
almost there
ok so now fix it and make it variable assignment (in code)
woooo
You should find the object by its type, which is, hopefully, PlayerController, in this case
frankly it's been hard enough getting them to do it the scuffed way lol
at least this way visibly guarantees the tags are in the right order...
This is a worse way to do it.
I agree
Which shouldn't be used by them
you're welcome to correct the mistake yourself
There is no need to find the player by its tag if it already has the PlayerController script, which I have then mentioned below
Right, that's what I was going to do
Replace this find method by FindAnyObjectOfType
And the T should be set to PlayerController
As you, hopefully, have a single PlayerController in your scene
Alright
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
T? what T? I don't see any T
was it a mistake?
I believe they're referring to the generic signature:
https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html
Where T in your case should be PlayerComponents
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; } }
https://gdl.space/zidomogodi.cs like this?
The SetPlayer method is defined in the correct spot but you need to have a script on the player that references their own player component and set themselves as a player in Start
These would not be on/in the PlayerManager script
These two need to be on the players and not the player manager
Can you show me the player component script?
I thought there was a PlayerComponents script?
oh right
like this?
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
should i remove what i put there?
Most likely 🤷♂️
now i put it there
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)
this is getting complicated for me
Well, what's the current issue?
what about these though?
I don't know anything about the second image.
For the first image my response was #1237752727564582952 message
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.
but then that will happen I am unable to assign them
When would that happen? (I know the answer but I am leading you to think about it)
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
because without the assigning, then the respawning player won't work and the player object won't be in the player combonents slot
If they're in the scene they'll call their start methods
As long as you can assure that they're in the scene they'll assign themselves during Start
they are in the next scene
||I don't fix code that isn't an issue or doesn't have a question to go along with it||
i'm supposed to have that type of Yourfunction right?
also, what do i do next?
What's your current task (objective/issue)?
I don't know anything about those lines of code.
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.
i playtested a bit, and it seems to be working, but let's see what the others think about it
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.
I think I forget to tell you that. removed these lines of code
also while I was taking a break from it, i made some tiles for the world Sweetland for the full one (
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
like this?
That's exactly the same code, so yes
Ok
this
imma show it here, when i got mentioned about it here, idk why but i just feel like it
is this where we talked about?
