The question is answered, right answer was accepted
hi, i need some help with a puzzle script
hi, i'm currently trying to make a 2D mechanic game and i was using a click and drag script but when i try to restart a level or in this case move to a new scene the script for enable it turn on automatically , this code should only turn on if the 2 others script condition is true, is there any way to fix this?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class acidnexttutorial : MonoBehaviour
{
public GameObject acid, acidshadow, dialog1,dialog2,dialog3, back;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (akifunnel.done == true)
{
acid.gameObject.SetActive(true);
acidshadow.gameObject.SetActive(true);
dialog1.gameObject.SetActive(false);
dialog2.gameObject.SetActive(true);
}
if (acidlock.done == true)
{
back.gameObject.SetActive(true);
dialog2.gameObject.SetActive(false);
dialog3.gameObject.SetActive(true);
}
}
}
[1]: /storage/temp/167605-error-script.png
@Wira38, I understand you have a problem, but it's not clear what you mean. Are you trying to set values in one scene and have them carry over to affect the next scene (or on reload of a scene?)
Answer by Wira38 · Sep 16, 2020 at 06:45 AM
To be clear, the script above will enable a gameobject if the drag and drop script(example the akifunnel) "done" bool value is true, but the problem here is when trying to replay the level or move to another scene the script automatically enable the object and disable the drag and drop script
Answer by streeetwalker · Sep 16, 2020 at 06:52 AM
@Wira38, it doesn't matter whether you are talking about loading a different scene or reloading the same scene, all variables are reset to the starting values for the scene. Reloading a scene is just like moving to a new scene. The newly loaded scene knows nothing about the previous scene.
If you want values to persist from scene to scene so you can set up the loaded scene based on what happened in the previously loaded scene, there are several ways to do it. Here is an easy method that has some drawbacks, and you should google about persistent/keeping values between scenes to learn more about why and what some potentially better alternatives are.
Create a static class in a script file. The static class will keep its values across scenes. Do not attempt to attach this to any game object - it will simply sit in your project folder and will be accessible from any class script in any scene.
For example
public static class GlobalValues {
public static bool hasGun = false;
}
// now from any script in any scene you can get or set hasGun
GlobalValues.hasGun = true;
// if you set hasGun in one scene, you can read the value in another.
Please note that this method does not keep data if you quit the game and restart. If you need to save data between game loads, then do a search and you will find a number of methods that involve serializing your game data and saving it to a file.