Hey all.
I’ve got a Game Over screen that I’m instantiating from a prefab when my player dies.
In the Game Over screen’s script I need to change a variable in a script that’s attached to the player. How should I do this? The editor won’t let me drag and drop the player onto the public variable in the Game Over script unless I first manually add an instance of the Game Over screen prefab into the scene. But then when I do that and drag the player over the variable I can’t get the Game Over screen prefab to save it.
I know there’s a GameObject.Find function, and I’m guessing I could use that to grab a reference to the player,
but I’ve read that it should be avoided if possible. Is there another way to do this that I’m unfamiliar with?
Just assign it at runtime after you instantiate the game over screen.
public GameOverScript prefab; // drag and drop from assets folder
public Player thePlayer; // drag and drop from the scene
void GameOver() {
GameOverScript goInstance = Instantiate(prefab);
goInstance.player = thePlayer;
}
I don’t know how to do this. I can’t assign an existing object in-scene to a prefab using the inspector window like your code says, and dragging the prefab Player into the variable slot in the game over screen prefab doesn’t get the right reference.
Okay, managed to make it work using the GameObject.Find function. Like I said in my first post, I’m curious if there is another method and if it’s better than the Find function. Trying to wrap my brain around all the different ways of doing things in Unity.
For anyone else that finds this, remember that you can’t assign an in-scene object to a variable in a prefab using the inspector window since that object might not exist in another scene where the prefab is used.
@PraetorBlue , forgot to say thanks for responding earlier.
Can you describe what you want to change? Something like “gameOver=false” so he can’t move anymore? then maybe the order is a bit wrong. My “feeling” tells me that a UI should not do anything in the data but the other way around.
Another a bit advanced idea would be to have an event manager. The player could subscribe to the “GAME_OVER” event and the GameOver screen could fire that event so the player gets notified by the event manager without the screen knowing about the player at all.
I literally shared a code example of how to do it with you. Not exactly sure how to be more clear about it. Nothing in my code example says to assign anything to a field on a prefab.
There’s a “Retry” button in the Game Over Screen that needs to change some variables in a script attached to the player when the button is clicked. That way the player can respawn.
Yes, that’s probably a better idea. This is my first game, so I’m very much stumbling around ‘in the dark’ so to speak.
Oh, I am a stumbler in the dark as well, it never seems to stop But since you know where to ask you will be a pro in no time
About the retry button: do you have some kind of GameManager that is a singleton maybe? Where do you detect that the game is over and “who” opens the Game Over Screen?
Two more ideas: once the player clicks “Retry” the Screen calls a function on the GameManager like RestartGame or RespawnPlayer. And the GameManager should know about the player and could make all the arrangements to restart the game for example reset the level, points counter, respawn enemies, etc.
Or if the GameManager knows about the GameOverScreen and and makes something like: gameOverScreen.SetActive(true)? Then you could make a script called GameOverScreen and put it on the game object of the screen, make a method in the script called “Show()” for example and then the game manager calls gameOverScreen.Show() and that method sets the gameobject active. Why that way around? Now you can maybe change the Show method to something like ShowForPlayer and the game manager calls ShowForPlayer(player) so the game over screen script can be configured to know about the player, maybe add other config methods as well to set the background color etc. IF you need that screen in different scenarios.