I have an empty game object that contains the two objects for my player as its children. I have a script that is on the empty parent that calls DontDestroyOnLoad, however I get the error: “DontDestroyOnLoad only works for root GameObjects or components on root GameObjects.”
I’ve tried putting DontDestroyOnLoad in Awake, but still nothing.
Here’s my script if it helps:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System;
public class PlayerPresence : MonoBehaviour {
public GameObject countdown;
bool spawned;
bool alive;
bool sceneChangeTrigger;
Scene currentScene;
bool toggleable;
private int lives;
private int currentLevelChoice;
// Use this for initialization
void Start ()
{
currentScene = SceneManager.GetActiveScene();
toggleable = false;
sceneChangeTrigger = false;
alive = true;
spawned = false;
lives = 3;
GetComponentInChildren<Camera>().enabled = false;
currentLevelChoice = GameObject.FindGameObjectWithTag("DontDestroy").GetComponent<HoldValues>().GetLevelChoice() + 3;
Destroy(GameObject.FindGameObjectWithTag("DontDestroy"));
DontDestroyOnLoad(this.gameObject);
}
// Update is called once per frame
void Update ()
{
if (SceneManager.GetActiveScene().name == "SpinMap" ||
SceneManager.GetActiveScene().name == "Shelter")
{
if (!sceneChangeTrigger)
{
sceneChangeTrigger = true;
CountDown();
}
}
if (Input.GetKeyUp(KeyCode.Return) && currentScene.name == "CharacterSelect")
{
SceneManager.LoadScene(currentLevelChoice);
}
currentScene = SceneManager.GetActiveScene();
if (toggleable && currentScene.name == "CharacterSelect")
{
toggleable = false;
ToggleMaterialChange();
TurnOffFixRotation();
}
if(currentScene.name != "CharacterSelect" && !toggleable)
{
ToggleMaterialChange();
TurnOnFixRotation();
TurnOnMovement();
toggleable = true;
}
if(!spawned && currentScene.buildIndex > 2)
{
spawned = false;
Spawn();
}
}
void Spawn()
{
if(lives != 0 && SceneManager.GetActiveScene().buildIndex > 2)
{
GameObject[] spawnPoints = GameObject.FindGameObjectsWithTag("Respawn");
float shortestDistance = Vector3.Distance(Vector3.zero, spawnPoints[0].GetComponent<Transform>().position);
for (int i = 1; i < spawnPoints.Length; i++)
{
if (shortestDistance > Vector3.Distance(Vector3.zero, spawnPoints*.GetComponent<Transform>().position) &&*
!spawnPoints*.GetComponent().GetState())*
transform.position = spawnPoints*.GetComponent().position;*
}
if(!spawnPoints[0].GetComponent().GetState())
transform.position = spawnPoints[0].GetComponent().position;
}
}
void TurnOffMovement()
{
GetComponentInChildren().enabled = false;
GetComponentInChildren().enabled = false;
}
void TurnOnMovement()
{
GetComponentInChildren().enabled = true;
GetComponentInChildren().enabled = true;
}
public void Destroy()
{
Destroy(this.gameObject);
}
public void ToggleMaterialChange()
{
ChangeMaterial[] enableds = GetComponentsInChildren();
foreach(ChangeMaterial mat in enableds)
{
if (mat.enabled)
mat.enabled = false;
else if (!mat.enabled)
mat.enabled = true;
}
}
void TurnOffFixRotation()
{
GetComponentInChildren().enabled = false;
}
void TurnOnFixRotation()
{
GetComponentInChildren().enabled = true;
}
public void CountDown()
{
StartCoroutine(StartGame());
}
IEnumerator StartGame()
{
GetComponentInChildren().enabled = true;
GameObject clone = Instantiate(countdown, countdown.GetComponent().position, Quaternion.identity) as GameObject;
clone.GetComponent().enabled = true;
int i = 5;
while (i < 5)
{
clone.GetComponent().text = i.ToString();
yield return new WaitForSeconds(1f);
i++;
}
clone.GetComponent().enabled = false;
}
}