- Home /
The question is answered, right answer was accepted
Issues with player prefabs when I try to teleport it
The player in my 2d game has the ability to teleport to a checkpoint(the checkpoint can be wherever he wants, what im doing is that I created a prefab of the player so, the player is just an instance of that prefab and, when the player presses E it destroys his gameobject and instanciates another one from the same prefab
if(canEgg == true && hasEgg == false)
{
Instantiate(eggPrefab, nuggetsPrefab.transform.position, nuggetsPrefab.transform.rotation);
canEgg = false;
hasEgg = true;
}
if (canEgg == true && hasEgg == true)
{
Destroy(gameObject);
canEgg = false;
hasEgg = false;
Instantiate(nuggetsPrefab, eggPrefab.transform.position, eggPrefab.transform.rotation);
}
Just for clarification, the player is a chicken, im in a 2d space, and the checkpoints are eggs, what I did there is that if the player presses E he creates a checkpoint, and if he presses E again he ""teleports"" to that checkpoint (dies and spawn another instance)
But when i play the game, the new player instance does not have activated the script or any components, just the sprite renderer(and the camera is a child of the player so it follows the player, but when the player is killed, it says there is no camera, even if the new player also has a maincamera child
Answer by logicandchaos · Feb 23, 2021 at 01:41 PM
instead of destroying and creating a new one just change its position.
instead of this:
Destroy(gameObject);
canEgg = false;
hasEgg = false;
Instantiate(nuggetsPrefab, eggPrefab.transform.position, eggPrefab.transform.rotation);
do this:
gameObject.position=eggPrefab.transform.position;
gameObject.rotation=eggPrefab.transform.rotation;
canEgg = false;
hasEgg = false;
and how do i make it so the player teleports to the instance that i created instead of the position and rotation of the prefab