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.