- Home /
Moving a gameObject to the position of an empty gameObject
I am exporting to flash and reloading the level when you die takes too much time. So, i am just moving the player back to the beginning point when you die.
I hard coded the position to send the player but that way i would have to re-write the script for each level to match the beginning points. So i am trying to save the position as a private variable.
So i have an empty game object at the beginning point of the level and when the player is in a situation that would cause death he moves to that position. However, there are different erros when i try different things. Below is the code i am using and i'll put the error up for the currently problem with this attempt.
#pragma strict
var enableGUI : boolean = false;
private var respawnPoint : Transform;
function Start () {
respawnPoint = gameObject.FindWithTag("respawnPoint").transform;
}
function OnTriggerEnter (hitPlayer : Collider) {
if(hitPlayer.gameObject.tag == "Player") {
enableGUI = true;
Time.timeScale = 0;
hitPlayer.transform.position = Vector3(respawnPoint.transform.position.x, respawnPoint.transform.position.y, respawnPoint.transform.position.z);
}
}
I am getting Null Exception Reference... but not to sure why since i have set the variable in start...
HELP! Frustrating.
Answer by yusufulutas · Apr 16, 2012 at 03:39 PM
You try respawnPoint = GameObject.FindWithTag("respawnPoint").transform; not gameObject, true is GameObject; and check tag.
Still getting the Null Exception on the 'respawnPoint =' and also the same error on the line 'hitPlayer.transform.position.
Sorry, don't worry i was being a tit. Forgot to add the actually tag onto the respawnPoint game object. Thank you though.
okay, you check again tag and check case sensitivity. you try hitPlayer.gameObject.transform.position
Answer by TheFrankman123 · Apr 16, 2012 at 04:52 PM
For future reference if anyone google searches this there is a better/easier way to do this:
You can just do it as:
function OnTriggerEnter (hitPlayer : Collider) {
if(hitPlayer.gameObject.tag == "Player") {
hitPlayer.transform.position = respawnPoint.transform.position;
}
}
Instead of defining all the axis separately.
Your answer
Follow this Question
Related Questions
OnTriggerEnter forcefield help! 1 Answer
Respawning a player after a certain amount of time 0 Answers
[solved] respawn when destroyed 1 Answer
How to make object move smoothly. 3 Answers
Trouble with a respawn script 1 Answer