Assigned Variable not saved with scene.

Hi all!

I was just trying to make a script that assigned a volume profile for my scenes. It looks like it works but then it when I open the same scene back up, it’s as though it’s never been assigned.

Does anyone know what I’m doing wrong? If I manually assign it works fine.
I did notice that next to the variable in the editor it comes up with (instance), could that have something to do with it?

Thanks!
Pete

    public Volume volume;
    public VolumeProfile volumeProfile;
    private void OnValidate()
    {
        if (doIt)
        {
            doIt = false;
            volume.profile = volumeProfile;
        }
    }

7877581--1001728--Feb-08-2022 13-58-11.gif

You probably need to mark the object dirty. See: Unity - Scripting API: EditorUtility.SetDirty

Hmm, I couldn’t get that to work.

    public Volume volume;
    public VolumeProfile volumeProfile;
    public bool doIt;
    private void OnValidate()
    {
        if (doIt)
        {
            doIt = false;
            volume.profile = volumeProfile;
            EditorUtility.SetDirty(this.gameObject);
            EditorUtility.SetDirty(volume);
            EditorUtility.SetDirty(volume.profile);
            volume.profile.isDirty = true;
        }
    }

It still just doesn’t stick. :frowning:

Where are you getting that VolumeProfile instance from. It might be what isn’t already saved to the scene… it’s subtle how assets are glommed together, and if something is not saved to disk then references to it also cannot be saved to disk.

It’s also order dependent. If you create a Material, assign it to a Renderer, THEN write the Material to disk, the reference in the Renderer will not serialize.

But if you write that same exact Material as an asset on disk first, THEN you can use it all day long in scenes / prefabs and it will persist as long as you dirty the ref.

Thanks Kurt,
The profiles have been saved earlier and are just sitting in the project. In the gif you can see the reference is assigned in the top script and it’s just trying to assign that to the volume script. I thought that (Instance) message might be a clue but I’m really not sure.
I made it into a little package that I might send off to support, if you feel like having a look (but no worries if not).

7877728–1001791–Volume Issue.unitypackage.zip (4.19 KB)

Was about to write similar, as I noticed the ‘(Instance)’ comes up when you hit the check box, implying you’re putting a transient copy of what looks like a scriptable object into the ‘Profile’ Field.

Also this seems to be a rather awkward way to do editor scripting, if that’s what you’re doing?

Hey Spiney,
The volume profiles are scriptable objects. So I just have one referenced that I want to apply to the volume through a script.
Oh yeah, and the script is pretty rough, I’m just trying to simplify it as much as possible to rule out anything else causing it to break.

Yeah, it’s a bit odd. It seems like it must be something to do with how it’s being referenced.
7877758--1001794--Feb-08-2022 15-40-33.gif
If I manually assign it the (instance) goes away and it saves fine.

Is that Volume component your own component or a Unity one?

If the former, I see a custom editor on Volume component; perhaps that’s the cause of the issue. Perhaps disable it and see if the issue persists?

Hell it being the latter could be the issue too. Though I am shooting in the dark here.

That’s a Unity component @spiney . It’s just how they handle post processing in URP.
I think I have it! I switched to debug mode -
7877872--1001824--Screen Shot 2022-02-08 at 4.50.55 pm.png
Looks like shared profile is the one, I was setting the wrong thing :roll_eyes: Yeay! :stuck_out_tongue:
Thanks for the help!
P

    public Volume volume;
    public VolumeProfile volumeProfile;
    public bool doIt;
    private void OnValidate()
    {
        if (doIt)
        {
            doIt = false;
            volume.sharedProfile = volumeProfile;
            EditorUtility.SetDirty(volume);
        }
    }
2 Likes