- Home /
Enable prefabs in different scene on level load
Hi guys,
I am making a 2D game and have 3 scenes. First is menu. Second is levels. Third is level 1. I have one level in the levels scene that is interactable. The rest are not interactable. I wrote a script for when level 1 is loaded the 2nd level in the levels scene is interactable unlocking the level. The problem is when i test this and load level 1 and go back to the levels scene the level 2 button is still not interactable. I attached the script to my camera in level 1 and used the button prefab for the script. But still nothing works.
Script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LevelUnlocker : MonoBehaviour {
public int scenes;
public Button level;
void OnLevelWasLoaded(int scene) {
if (scene == scenes)
level.interactable = true;
}
}
Answer by nickostan · Feb 22, 2016 at 04:32 AM
You need to use the DontDestroyOnLoad(); function.
It allows gameobjects to persist between scenes.
http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
A good live training session on explaining this can be found here:
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/creating-a-scene-menu
Answer by bitstrider · Feb 22, 2016 at 09:53 AM
If you want to persist data across scenes, you could try PlayerPrefs. GameObjects are thrown away when you leave the scene they originated from, so you can't use them like this.
Your answer
![](https://koobas.hobune.stream/wayback/20220612065045im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
C# UI list item drag onto 2d sprite in world space? 1 Answer
Help check click out of UI 2 Answers