- Home /
Respawning objects upon player death problems
Ok, so I'm making a puzzle game where you move the objects in the environment about to progress. Because they're so important, I need to make sure they aren't permanently missable. So I added code to make it that if an object drops off the edge of the screen, they respawn where they first started the level.
However, I also wanted to make it so that when the player died, all the moved objects would respawn in their starting locations. However, the codee I've written to do so crashes Unity everytime it's activated. I think it might be due to the workload required from the code (which is a real problem for my project if so, I'm a bit worried), but just in case it's a coding issue, I thought I'd post this here. Perhaps my code is causing it?
Anyway, here is the code. Upon falling into the pit, an OnDeath message is sent to the main character.
This activates this code on the characters, which searches for moveable game objects and sends a death message out to them too.
function OnDeath () {
var moveables : GameObject[];
moveables = GameObject.FindGameObjectsWithTag("Moveable");
//Moveable objects are all tagged as such, this script searches for them.
for (var moveable in moveables)
{SendMessage ("OnDeath", SendMessageOptions.DontRequireReceiver);
}
Spawn ();
//This line respawns the character themselves
}
In turn, when the objects recieve this message, this script is activated.
private var spawnPos : Vector3; private var spawnRot : Quaternion;
private function Awake() { spawnPos = transform.position; spawnRot = transform.rotation; //Debug.Log (spawnPos); //Debug.Log (spawnRot); }
function OnDeath () { transform.position = spawnPos; transform.rotation = spawnRot; }
At the moment, there are about 20 moveable objects in my scene, but in my final game, this is liable to become a lot more. Am I going to be in trouble here?
P.S. it'd definetely one of these sections of code causing the crashes, it happens literally the second you die.
I at the moment have no time to really look into your code and track down the problem - I will later if it's not fixed yet by then. But just a little suggestion: Can't you just reload the scene upon player death? That would make the workload a lot smaller. It's perhaps not the nicest workaround, but by putting DontDestroyOnLoad on a few things you could probably make it work, if I understand the way your game works correctly.
This would of course imply that the 'moving stuff part' of your game takes place in a seperate scene, but by loading that scene upon entering a hallway for instance, it should be (I think) possible.
The moving stuff scene is my only scene currently, but reloading the scene sounds like it could be an option. The only problem I foresee is that I want the player to respawn at the last checkpoint, not the beginning of the scene each time. It sounds like this 'DontDestroyOnLoad might work, what exactly does it do?
Answer by Alienjesus · Apr 24, 2011 at 04:21 PM
Decided to reload the scene upon death, looking into using singletons to save checkpoints.
Answer by loramaru · May 04, 2011 at 04:08 AM
I believe your problem is an infinite recursion on the player's OnDeath handler. By calling SendMessage() without an explicit object, you are calling it on the current object, the player. Instead, you want...
for (var moveable in moveables)
{
moveable.SendMessage("OnDeath", SendMessageOptions.DontRequireReceiver);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613114242im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Two cameras and switching 2 Answers
Webplayer templates custom tags (and the player settings GUI) 0 Answers
Have something never lose momentum? 1 Answer
Trigger not registering imported objects 2 Answers
how many tags is too many 3 Answers