Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Scene variables on Don'tDestroyOnLoad "scene"

Discussion in 'Visual Scripting' started by Camilohoyos16, Oct 4, 2022.

?

How can I get visual scripting's scene variables from Don'tDestroyOnLoad "scene"

  1. Accessing from SceneVariables.Don'tDestroyOnLoad method

    1 vote(s)
    100.0%
  2. Get all possible scene variables, try with SceneVariables.AllVariables

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. Camilohoyos16

    Camilohoyos16

    Joined:
    Feb 11, 2017
    Posts:
    7
    I am saving scene variables from an object marked as "Don'tDestroyOnLoad", this is creating a new visual scripting scene variable's instance. Now I am trying to get these variables but SceneVariables.Instance just receives a Scene asset as parameter. I already searched on all the current active scenes and none of them have the variables.

    How can I access to these variables? Someone on internet said Visual Scripting creates a new "scene" called "Don'tDestroyOnLoad" but, as I mentioned before, SceneVariables.Instance just receives a scene asset and this "new scene" doesn't actually exists, so I can't gather info for that mysterious scene
     
  2. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    it creates a new Game Object in the hierarchy called don't destroy on load not a scene right?
    Capture1.PNG
    anyways this is the node you use to get saved variables:

    Capture.PNG
     
  3. Camilohoyos16

    Camilohoyos16

    Joined:
    Feb 11, 2017
    Posts:
    7
    Thanks for the answer, but notice I specified a different processes, like pass a scene asset as parameter, hence I am talking about get this variables from code. Sory whether I was not clear enough. Anyway, thanks again!.
     
  4. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    ah, sorry.
    didn't realize you're looking how to access the variables via script
     
  5. ericb_unity

    ericb_unity

    Unity Technologies

    Joined:
    Nov 24, 2017
    Posts:
    167
    Example script to set ActiveScene variable.

    Code (CSharp):
    1. using Unity.VisualScripting;
    2. using UnityEngine;
    3.  
    4. public class setsceneVariable : MonoBehaviour
    5. {
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         Variables.ActiveScene.Set("MySceneText", "test");
    10.     }
    11. }
    You can find documentation about it at: Variables node | Visual Scripting | 1.7.8 (unity3d.com)

    Scene variables are living in a GameObject named "VisualScripting SceneVariables" and are automatically added the first time a visualscript graph is added to a scene in the same scene as the GameObject. Having multiple scenes loaded additively, won't make let you see scenes variables from other scenes.
    upload_2022-10-5_15-14-13.png
     
    Last edited: Oct 5, 2022
    REDACT3D_ likes this.
  6. Camilohoyos16

    Camilohoyos16

    Joined:
    Feb 11, 2017
    Posts:
    7
    Yeah I saw that Doc and this doesn't have the answer.
    And sure, you can't access to variables for another scenes, but I am talking about DontDestroyOnLoad "scene". I put "" because that is not a real scene. I am adding a scene variable from jerbaroo(Clone) object. How can I access to those variables?. I already checked all scene variables from all active scenes and they are not anywhere.

    upload_2022-10-6_10-20-16.png
     
  7. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,793
    Scene Variables are scene specific - each scene has one such object. It shouldn't be marked as DontDestroyOnLoad. Use Application variables for scene independent variable tracking.
     
  8. Camilohoyos16

    Camilohoyos16

    Joined:
    Feb 11, 2017
    Posts:
    7
    Hey, thanks for the reply. I am not marking this as DontDestroyOnLoad, it has been created automatically there. So here is the question. If there is not a scene present, why has this been created at all?
    I used application as workaround, thanks for the suggestion.
    This should throws an error when you try to create a scene variable from a DontDestroyOnLoad object.
     
  9. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    I wonder if it could be destroyed after load? :p

    are you sure the values you are trying to access are not just the Visual scripting scene variables?
    those do exist before run time. like a normal var
    InkedCapture_LI.jpg
    Note the Scene tab here this is where those vars are stored ^
     
    Last edited: Oct 6, 2022
  10. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,793
    Weird indeed. Maybe a bug. My understanding is that Scene variables are or at least were in Bolt a scene scope singleton. Once a new scene is loaded additively, its Scene Variables overrides the previous scene variables as the current tracked reference. And they are destroyed on non-additive scene load.
     
  11. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    I always thought of scene variables as like just a pre-named object variable container. basically holds values like if you just made an emptyobject and attached object variables kinda thing.

    but for some reason it instantiates itself and changes the name so the user doesn't delete it on accident. otherwise it would have the same name? i dono

    it's clearly addin' somthin' as child objects ^.^
    Capture1.png
     
  12. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,793
    It essentially is that - a GameObject containing Variables component that's automatically created by UVS in every scene that has ScriptMachine or StateMachine component somewhere in Hierarchy.

    The few ways it's different from Object variables is that UVS references this scene variables GO automatically and Get/Set Scene Variable nodes don't have GameObject targets because the system only references one instance of Scene Variables. Whenever a new scene is loaded additively with its own Scene Variables GO, the system internally then references the newly loaded Scene Variables because it expects only one Scene Variables instance to exist i.e. a scene scope singleton of sorts if you will.
     
    REDACT3D_ likes this.
  13. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    neato'
    always interesting stuff to talk about.

    why doesn't it just create an asset file in the project folder to store this game object?
    surely it could be generated at runtime from an external file
    just wondering why the need to spam the hierarchy even if you never use any scene variables?

    i imagine it's built like this because bolt. how it was like added-on
    and also, the canvas has that eventsystem but the canvas is a whole other topic too lol