#Additive gui scene

1 messages · Page 1 of 1 (latest)

rain tartan
#

I've been chatting with different people trying to fix this problem but haven't found a solution so far so I decided to create a thread...

#

I'm trying to set up 6 different scenes (which at the moment are blank and they just have the background image) and an additive scene (the scrollbar) which is usable in every other scene

#

I managed to add it but:

  1. I repetedly get an error in console
  2. it doesn't stay in position (it resets its cordinates every time I change scene
#

I just fixed the console error by removing the event system in the gui scene... but I still don't know how to fix the other problem

modest lodge
#

do you need a separate scene for each item?

#

do you have code that runs in the scrollbar scene that may cause the scripts to re-run?

#

when you unload a scene, store the scrollbar value, then, on scene load, assign the scrollbar value back. maybe it'll be fast enough to keep its current position . . .

rain tartan
#

only the buttons on the scrollbar have scripts

modest lodge
rain tartan
#

let me try

rain tartan
#

I've never worked with c# before, don't know anything about it

#

string hand = GameObject.Find("/GUI/Scroll/Panel").transform.position;

#

this doesn't seem to work

modest lodge
# rain tartan I've never worked with c# before, don't know anything about it

that's a big issue. smth you to program and make games with unity. it's hard to help someone without the basic/intro knowledge of c#, especially if they don't understand the help given

essentially, you need to subscribe to the load and unload events. add a method to each event: one that stores the scrollbar value on unload, and one that assigns scrollbar value to the stored value on load . . .

rain tartan
#

I'm a python master so I don't think I'll have a lot of problems understanding this

#

what I'm trying to say is that I understand the logic

#

I just don't know the correct syntax

#

I need to create a variable that stores the scrollbar's position, then check if the scene already changed and then change the scrollbar's position with the variable I created before

rain tartan
#

but it doesn't seem to find the scrollbar

modest lodge
#

first, you need to create a script to handle all of this and place it on a GameObject in the scene . . .

rain tartan
#

so I don't have to associate the script to the scrollbar?

modest lodge
#

since it will be handling the scrollbar position . . .

#

you can place it on the scrollbar GameObject, a parent GameObject, or a separate handler/manager GameObject in the ui scene . . .

rain tartan
#

alr

modest lodge
#

no need to use Find. your script will have a variable to reference the Scrollbar component to access its members . . .

rain tartan
#

ok

modest lodge
#

yep. now subscribe to the events from the SceneManager link . . .

modest lodge
#

are you unloading and loading your scenes?

rain tartan
#

I guess so

#

just jumping between scenes

#

which I think it means I'm unloading a scene and loading another one

modest lodge
#

ok, but what method do you call/execute to do that?

rain tartan
#

yeah yeah, just opened the changescene script, I'm loading them

#

SceneManager.LoadScene(sceneID);

#

but not unloading, is that something I should do?

modest lodge
#

it's fine, but you want to use the other two events instead of activeSceneChanged . . .

rain tartan
#

oh ok

rain tartan
modest lodge
#

a scene is unloaded before a new one is loaded. the script listens for the unload event to store the position. it also listens for the load event to assign the position back . . .

rain tartan
#

alright

#

how to do that

#

lol

#

I'm really sorry

#

but I've been reading the documentations until now and I don't understand anything

modest lodge
#

click on the link for the events. they have example code . . .

rain tartan
#

I'm looking at this

#

I just don't get how to use it in an if statement

modest lodge
#

that is what you will do with both of the load events . . .

rain tartan
#

I don't have a camera

#

I can't have one

#

not in the gui scene at least

#

because it makes a conflict with the cameras of other scenes

modest lodge
#

huh? you don't need one . . .

rain tartan
modest lodge
#

it's an example. you shows you how to set it up and subscribe to an event. that's the only part you need . . .

rain tartan
#

hum

#

let me try to watch it again

#

my brain is slowly turning off

modest lodge
#

this is a snippet from the link i sent. this is how you subscribe and unsubscribe from an event . . .

rain tartan
#

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class KeepPosition : MonoBehaviour

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class KeepPosition : MonoBehaviour
{
    void OnEnable()
    {
        SceneManager.sceneUnloaded += OnSceneUnloaded;
    }


    void OnDisable()
    {
        SceneManager.sceneUnloaded -= OnSceneUnloaded;
    }
    private void OnSceneUnloaded(Scene current)
    {
        Debug.Log("OnSceneUnloaded: " + current);
    }

    void keepPosition()
    {
        Vector3 pos = transform.position;
        pos.y = Random.Range(1.0f, 3.0f);
        transform.position = pos;
    }
}
#

does this make any sense?

modest lodge
#

the method that is called from the event is where you place your code. Teleport moves the object to a different position. your code will assign the value of the scrollbar in one method, and store the value of the scrollbar in a different method (used with the other event) . . .

modest lodge
rain tartan
#

ok

#

done

#
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class KeepPosition : MonoBehaviour
{
    void OnEnable()
    {
        SceneManager.sceneUnloaded += OnSceneUnloaded;
    }


    void OnDisable()
    {
        SceneManager.sceneUnloaded -= OnSceneUnloaded;
    }
    private void OnSceneUnloaded(Scene current)
    {
        Vector3 pos = transform.position;
        transform.position = pos;
    }
}
#

I don't get what those:
SceneManager.sceneUnloaded -= OnSceneUnloaded
SceneManager.sceneUnloaded += OnSceneUnloaded;
are supposed to do

modest lodge
#

the code within the method has nothing to do with the scrollbar. but before fixing that, you need to do the same thing when a scene is loaded . . .

modest lodge
rain tartan
#

oh okay

#

thanks

modest lodge
#
SceneManager.sceneUnloaded -= OnSceneUnloaded```
this removes the delegate (your method) from the list of subscribers contained in the event . . .
rain tartan
#
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using System;

public class KeepPosition : MonoBehaviour
{
    void OnEnable()
    {
        SceneManager.sceneUnloaded += OnSceneUnloaded;
        SceneManager.sceneLoaded -= OnSceneLoaded;
    }

    void OnDisable()
    {
        SceneManager.sceneUnloaded -= OnSceneUnloaded;
        SceneManager.sceneLoaded += OnSceneLoaded;
    }
    private void OnSceneUnloaded(Scene current)
    {
        Vector3 pos = transform.position;
    }
    private void OnSceneLoaded(Scene current)
    {
        transform.position = pos;
    }
}
#

does this make sense then?

modest lodge
#

just place a log in each method so you can see the logs and the order of execution from the console. remove the position stuff. that is just saving the GameObject position which has nothing to do with the scrollbar value . . .

rain tartan
#

doesn't it?

modest lodge
#

no . . .

rain tartan
#

aren't we trying to store the coordinates of the gameobject?

modest lodge
#

the GameObject position as nothing to do with the scrollbar component and its members . . .

#

you're not moving the GameObject, you're moving the value property of the Scrollbar component. the component is attached to a GameObject. they are not the same thing . . .

rain tartan
#

right

#

I'm starting to understand

#
private void OnSceneUnloaded(Scene current)
    {
        Debug.Log("Unloading scene");
    }
    private void OnSceneLoaded(Scene current)
    {
        Debug.Log("Loading scene");
    }

correct?
modest lodge
#

yeah, change scenes and test if they're called in the proper order . . .

rain tartan
#

but I've a problem

rain tartan
#

so

#

what next

modest lodge
#

just look at the other one . . .

rain tartan
#

they have to be both + and then both -?

modest lodge
#

your'e trying to remove the subscription when enabled and add it when disabled. you can't remove smth that wasn't added in the first place . . .

rain tartan
#

makes sense

#

so

#

what do I do to test if it works?

modest lodge
#

you can't. you have errors . . .

rain tartan
#

fixed them

modest lodge
rain tartan
#

I had to add LoadSceneMode mode)

modest lodge
#

make sure you have the correct parameters . . .

rain tartan
modest lodge
#

attach what?

rain tartan
#

the script

#

it's attached to a gameobject rn

modest lodge
#

if it's not attached to a script, how can it run? have you looked at any videos or tutorials? how'd you get your other stuff to run?

#

also, didn't you already do that?

rain tartan
#

yeah yeah

#

I was just searching for a confirm

#

I get nothing in my console

modest lodge
#

did you change scenes? did you put the script on a GameObject?

rain tartan
#

nvm

#

yeah yeah

#

I had console logs disabled

modest lodge
#

did you click the button to view logs from the console?

#

figured . . .

rain tartan
#

I'm getting only "loading scene" though

#

that's probably because in my changescene script I don't unload the scene, I just load another one

modest lodge
#

the example doesn't show needing the unload, so i took a change it'd work . . .

#

instead, unload the scene, then load the next scene in your code and see if that works . . .

rain tartan
#

yep

#

I was trying that in the meanwhile

modest lodge
#

that was my original intention to begin with . . .

rain tartan
#

so now I just need to change what happens in the methods

modest lodge
#

you store the scrollbar value when the scene is unloaded, and assign it back to the scrollbar value when the scene is loaded . . .

rain tartan
#

I have no clue on how to do that

#

how do I tell him to get the scrollbar's position?

modest lodge
#

it should be similar in any programming language. how would you access a field/member of an object in python?

rain tartan
#

but I don't know how to access that value

#

like what's the variable's name

#

I thought that the find function could help me

modest lodge
#

then you need to look up the Scrollbar component from the unity scripting API to look at its members: fields, properties, methods, events . . .

rain tartan
#

hum

modest lodge
#

no, you have to read the description/definition of the Find method. it only finds and returns a GameObject based on the name provided . . .

#

you need to create a variable to reference the Scrollbar component . . .

rain tartan
#

what if I add:
GameObject scrollbar;

and then I add:
Vector3 pos = scrollbar.transform.position;

to the OnSceneUnloaded method

#

this should work, right?

rain tartan
#

this is what I'd do in python

modest lodge
#

you created a GameObject type named scrollbar. so scrollbar references a GameObject component. you need to reference a Scrollbar component . . .

rain tartan
#

hum

#

but the scrollbar isn't just a GameObject?

modest lodge
#

everything is a GameObject. only GameObjects exists in a scene, but a GameObject is just a container storing a list of components . . .

rain tartan
#

oh ok

#

I get it

modest lodge
#

every GameObject has a Transform component by default which is why each GameObject you lcik on has one at the top . . .

#

this is all stuff you learn at the beginning from tutorials and would have made this much simpler . . .

rain tartan
#

ah

#

ups

#

I guess I'll watch some tutorial later on

#

I need to access the Pos X value then

#

any idea on how to do that?

modest lodge
#

no, do you even know where the Scrollbar component is?

rain tartan
#

I didn't add a scrollbar component

#

oh wait

#

is it scroll rect?

modest lodge
#

how are you scrolling through the images?

rain tartan
#

the hierarchy is Scroll/Panel/(icons)

modest lodge
#

okay, so it's a ScrollRect, not a Scrollbar. that's the type/component you need to make your variable, then you can reference it in the scene to access its members . . .

rain tartan
#

Ok, give me a couple minutes and I’ll try that

rain tartan
#

like this?

#

GameObject ScrollRect;

#

and this on top

modest lodge
#

no, this is the same thing you've been doing before. you only changed the name of the variable. it's still a GameObject component . . .

#

a GameObject position is what moves in space of the scene. you are not physically moving a GameObject in the scene. you are changing/updating the value of a memeber (either a field or a property) that belongs to a ScrollRect component . . .

#

how do you make an integer variable?

rain tartan
#
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using System;
using static UnityEditor.PlayerSettings;
using UnityEngine.UI;

public class KeepPosition : MonoBehaviour
{
    ScrollRect Scrollbar;
    Vector3 pos;
    void OnEnable()
    {
        SceneManager.sceneUnloaded += OnSceneUnloaded;
        SceneManager.sceneLoaded += OnSceneLoaded;
    }

    void OnDisable()
    {
        SceneManager.sceneUnloaded -= OnSceneUnloaded;
        SceneManager.sceneLoaded -= OnSceneLoaded;
    }
    void OnSceneUnloaded(Scene current)
    {
        Debug.Log("Unloading scene");
        Vector3 pos = Scrollbar.transform.position;
    }
    void OnSceneLoaded(Scene current, LoadSceneMode mode)
    {
        Debug.Log("Loading scene");
        Scrollbar.transform.position = pos;
    }
}
modest lodge
#

the type is correct, but for the last time: stop getting the GameObject position. i don't know how else to say it. you are not moving the GameObject in the scene. you are changing a value from the ScrollRect component . . .

rain tartan
#

lol

#

I understand that

#

now at least

#

but how

modest lodge
#

you need to look at the API for ScrollRect to find the member: field or property, that stores the position . . .

modest lodge
#

bingo . . .

rain tartan
#

LESGO

#

Ahahahaha

#

Can’t access the pc for a couple hours, I’ll test as soon as I can

rain tartan
#

I'm back

rain tartan
modest lodge
#

just try storing the normalizedPosition to a Vector2 variable when the scene unloads, then assign that stored variable back to normalizedPosition and see if it works . . .

modest lodge
#

and it still popped?

rain tartan
#
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using System;
using static UnityEditor.PlayerSettings;
using UnityEngine.UI;

public class KeepPosition : MonoBehaviour
{
    public ScrollRect Scrollbar;
    public Vector3 position;

    //private void Awake()
    //{
    //    DontDestroyOnLoad(this.gameObject);
    //}
    void OnEnable()
    {
        SceneManager.sceneUnloaded += OnSceneUnloaded;
        SceneManager.sceneLoaded += OnSceneLoaded;
    }

    void OnDisable()
    {
        SceneManager.sceneUnloaded -= OnSceneUnloaded;
        SceneManager.sceneLoaded -= OnSceneLoaded;
    }
    void OnSceneUnloaded(Scene current)
    {
        Debug.Log("Unloading scene");
        position = Scrollbar.normalizedPosition;
    }
    void OnSceneLoaded(Scene current, LoadSceneMode mode)
    {
        Debug.Log("Loading scene");
        Scrollbar.normalizedPosition = position;
    }
}
modest lodge
#

change the varaible type to Vector2. also, log the value of normalizedPosition in both of the methods to see their values and test it . . .

#

is position assigned in the inspector at all when you load a scene?

rain tartan
#

it does print the correct values when unloading

#

but it resets them to 0 after the loading

#

the other guys told me that the script "destroys" itself when I unload a scene or something like that

modest lodge
#

try putting the DontDestroyOnLoad on it (i see in the comments) . . .

rain tartan
#

yeah

#

nothing changed

modest lodge
#

it has to be on the parent GameObject, so try transform.root.gameObject as the argument for it . . .

rain tartan
#

yeah

#

now it "worked"

#

it creates the DontDestroyOnLand on the left

rocky bane
rain tartan
rain tartan
rocky bane
#

Now, when you said it creates the DDOL scene, did it also put the MainCanvas IN that scene?

rain tartan
#

let me see

#

yeah

#

everything

#

and the additive scene is empty

#

OOOOO

#

IT WORKS

#

I removed this line from the changescene script:

SceneManager.LoadScene(8, LoadSceneMode.Additive);

#

apparently the DDOL and the additive scene where having a contrast

rocky bane
#

Ok nice.
Another alternative, since you're already doing Additive scene loading, is just keep the MainCanvas on a base scene that isn't unloaded

rain tartan
#

but now how do I tell him where I want it to be there and where I don't?

rocky bane
rain tartan
#

dang

rocky bane
#

You have to handle that in code outside of the scene structure

#

Or do a basescene for when you're in the "set" of scenes that need the canvas and have it live there. Then only when you DON'T need it AT ALL, remove that scene.

Or, like I said earlier, just have a script with values in DDOL. And draw from it when needed, so the MainCanvas can be unloaded and loaded safely whenever you want

rain tartan
#

oh, the script works just like this:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using System;
using static UnityEditor.PlayerSettings;
using UnityEngine.UI;

public class KeepPosition : MonoBehaviour
{
    public ScrollRect Scrollbar;
    public Vector2 position;

    private void Awake()
    {
        DontDestroyOnLoad(this.transform.root.gameObject);
    }
}
modest lodge
#

you don't need this . . .

rain tartan
#

oh ok

#

if(SceneManager.GetSceneByName == "test")
{
DontDestroyOnLoad(transform.root.gameObject);
}

#

I was trying to do something like this

#

does it make sense in first place?

rain tartan