Scene Manager and Keeping Objects when Loading/Unloading + Gameogject.SetActive not working:(
Hey! I'm in a bit a pickle. I'll tell you what I'm trying to achieve first. It's a first-person "fighter game" where you're interacting with robots (one at a time) placed in different environments with separate HDRI maps. There should be a "base scene" that never unloads - containing its own camera, controller, score-manager, and the robots themselves (one robot per scene). If a robot was punched and fell down, it would trigger a new scene to appear containing a new HDRI map and new destroyable objects. The falling robot would land onto a pile of other robots in a separate display that's still part of the "base scene" and would stay there. The falling robot would trigger a new robot to appear (Set.Active) in the "base scene" as well. This is something that I was experimenting with, but couldn't figure out. I got the scene loading and unloading part working, but I can't seem to set new robots active. This script is for the trigger that robots falls through:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class acivateRobot : MonoBehaviour {
public GameObject RobotKyle;
private void Start()
{
RobotKyle = GameObject.FindGameObjectWithTag("Robot3");
RobotKyle.SetActive(false);
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Robot"))
RobotKyle.SetActive(true);
this.gameObject.SetActive(true);
Debug.Log("Kyle is active");
}
}
In addition to the script above, I'm running this manager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class AnyManager : MonoBehaviour {
public static AnyManager anyManager;
bool gameStart;
void Awake ()
{
if(!gameStart)
{
anyManager = this;
SceneManager.LoadSceneAsync(1, LoadSceneMode.Additive);
gameStart = true;
}
}
public void UnloadScene(int SceneNumber)
{
StartCoroutine(Unload(SceneNumber));
}
IEnumerator Unload(int SceneNumber)
{
yield return null;
SceneManager.UnloadSceneAsync(SceneNumber);
}
}
Ans two simple scripts to load and unload scenes if a robot were to fall through another trigger(attached to separate scenes).:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Load : MonoBehaviour
{
public int SceneNumber;
bool loaded;
private void OnTriggerEnter(Collider other)
{
if (!loaded)
{
SceneManager.LoadSceneAsync(SceneNumber, LoadSceneMode.Additive);
loaded = true;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Unload : MonoBehaviour
{
public int SceneNumber;
bool unloaded;
private void OnTriggerEnter(Collider other)
{
if (!unloaded)
unloaded = true;
AnyManager.anyManager.UnloadScene(SceneNumber);
}
}
I would greatly appreciate your help! Perhaps there's a better way to do this? Thanks!