- Home /
GameObject is spawned twice
Hello guys, i'm having some trouble spawning my player on my 2D game. The isse is that when i try to spawn my player on a spawn point, my player is spawned twice. That means that my Spawn function is called twice, I can't figure out why is that happening.
Here's the code:
using UnityEngine;
using System.Collections;
public class Respawn : MonoBehaviour {
public GameObject character;
public Transform ptoDeSpawn;
// Use this for initialization
void Start () {
SaveCheckPoint (ptoDeSpawn.position.x,ptoDeSpawn.position.y,ptoDeSpawn.position.z);
Vector3 pos = new Vector3 (PlayerPrefs.GetFloat("checkPointX"),PlayerPrefs.GetFloat("checkPointY"),PlayerPrefs.GetFloat("checkPointZ"));
Spawn(character,pos);
}
// Update is called once per frame
void Update () {
}
void SaveCheckPoint(float posX, float posY, float posZ){
PlayerPrefs.SetFloat ("checkPointX", posX);
PlayerPrefs.SetFloat ("checkPointY", posY);
PlayerPrefs.SetFloat ("checkPointZ", posZ);
}
void Spawn(GameObject character, Vector3 pto){
if (character) {
GameObject p = Instantiate (character, pto, Quaternion.identity) as GameObject;
CamaraController cam = Camera.main.gameObject.GetComponent<CamaraController> ();
cam.target = p.transform;
} else {
print("is null");
}
}
}
Thanks!
Do you have the script attached twice by any chance? Are you calling Spawn
from anywhere else?
Try adding a Debug.Log
call to the Spawn
method, it should help you work out where it's being called from. Something like Debug.Log("Spawning");
This will print the word "Spawning" to the Unity console. If you select it, you'll be able to see the stack trace for the debug output. That lets you work out where it's being called from, so you can see if it's being called anywhere other than the Start
method. If it's not, you must have two instances of the script somewhere.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142911im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Phantom Instantiation at Origin 0 Answers
Spawn origin point!! 0 Answers
How can I spawn and drag from a button click? 3 Answers
What is the best way to spawn enemies? 3 Answers
Respawn random objects 1 Answer