Working on my player prefab spawning at different save points after death. I am Instantiating the player at the new vector that is imported by the save point using UpdateSpawnPos method.
Problem I am having is warBallSpawnLocation vector seems to be resetting to the level start position I set at awake after the scene load. I have a singleton that I thought would preserve the SceneController and the warBallSpawnLocation new vector when it is inserted to the UpdateSpawnPos() but the instantiated prefab always starts at the default spawn location.
If anyone needs me to make notes in the code I would be happy to do that but I usually do that after its working the way it should. Any help would be great or if the code is fundamentally flawed let me know, Im always appreciative of feedback.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneController : MonoBehaviour
{
[SerializeField] GameObject warBallPrefab;
[SerializeField] GameObject cameraController;
private static Vector3 warBallSpawnLocation;
private void Awake()
{
int sceneControllerCount = FindObjectsOfType<SceneController>().Length;
if(sceneControllerCount > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
warBallSpawnLocation = new Vector3(72, 42, 205);
SpawnWarBall();
}
private void Update()
{
Debug.Log(warBallSpawnLocation);
}
public void ResetGame()
{
SceneManager.LoadScene(0);
SpawnWarBall();
}
public static void UpdateSpawnPos(Vector3 spawnPos)
{
Debug.Log(spawnPos);
warBallSpawnLocation = spawnPos;
}
public void SpawnWarBall()
{
cameraController = FindObjectOfType<CameraController>().gameObject;
GameObject warBall = Instantiate(warBallPrefab, warBallSpawnLocation, Quaternion.identity);
cameraController.transform.position = warBallSpawnLocation + CameraController.GetCameraOffset();
}
}