Help with exception to DontDestroyOnLoad
I'm having a bit of a complicated issue so bear with me while I try to explain it.
Each level in my game is divded into 3 seperate scenes. Each scene represents a different stage of "alert". (For example if the player steps in front of a security camera it triggers the "alarm" version of the level).
Im useing a basic DontDestroyOnLoad script attached to the player character in order to keep the players position during transition between these stages.
function Awake(){
DontDestroyOnLoad (this);
}
HOWEVER (this is where things start getting tricky to explain) on starting a new level, The player needs to move to the beginning of the level, which of course, is at a different position then the location where the last level ended.
I've tried inserting a second player character and using the destroy function on an empty game object in the level to destroy the old character but that doesn't seem to work for me. I just end up with two player chacters in the level.
function Start() {
Destroy(gameObject.Find("character1"));
}
Note: I also posted this question on the forum as I'm not entirely sure where it belongs.
Answer by Zoogyburger · Feb 28, 2016 at 02:13 AM
This will make sure the player is not destroyed or duplicated in c#:
if (!playerExists) {
playerExists = true;
DontDestroyOnLoad (transform.gameObject);
} else {
Destroy (gameObject);
}
I don't think I did a very good job explaining my problem. The problem isn't that the player is being destroyed. But rather that I need the player to be destroyed on exiting certain scenes but not others.
Answer by mrtyninja · Feb 28, 2016 at 03:18 PM
I figured out a temporary solution in case anyone else is having a similar issue. Its not the most elegant solution, But what I did was add an empty game object in the area where I want the player to start and created a box collider in the area where the player actually starts due to the DontDestroyOnLoad script. var destination : Transform;
function OnTriggerEnter(other : Collider) {
other.transform.position = destination.position;
}
Set the transform position to the empty game object and it works as a temporary fix.
Your answer
Follow this Question
Related Questions
Question on Unity5 CharacterController performance/efficiency 0 Answers
I can't rotate and move in the opposite direction with character Controller 0 Answers
CharacterController glitches if it reaches a ledge while jumping into a wall. 4 Answers
ANSWER REQUIRED NOW: inactive character controller with bolt 0 Answers