#GameObject becomes null at Run Time (created and referenced by an Editor Script)

1 messages · Page 1 of 1 (latest)

dapper bobcat
#

Hey there! Really weird issue, and I'm fairly new to Unity, so sorry if I don't describe it super well.

I'm working past the end of the Tower Defense tutorial, and I've been working on adding an in-Editor Editor, for making my own maps and whatnot. I managed to get most of it working, but I'm having a small issue.

Right now, I can click a button to make my path for me, and it'll add the Start, End, color the paths in between, and add all the right waypoints in the right order from a selection of nodes in the scene. Because I'm using a custom editor interface, I'm tracking what's been made so far. I have a private variables path that is for the gameObject that is the parent of all the path-related gameObjects being spawned, for easy sorting and cleanup. When I'm in the editor it keeps track of whether the path has been made or not, and disabled certain buttons based on that.

The problem is when I go to play, path goes to null, and my public getter PathIsMade returns false instead of true. Exiting playmode to the editor doesn't restore it either.

It's not a huge deal, with the way my editor script is set up I can fix the state-sync issue by hitting one or two more buttons, but I would like to get it figured out. I'm making this my learning project, so I don't want to leave any issue undone if I can help it.

Here's the code! Let me know if there's any more information I can add.

#
    ...

    void OnValidate()
    {
        Debug.Log(PathExists);
        Debug.Log(path);
    }
}

True when working on it in the editor, false in runtime, false after runtime, without the gameObject going away

quiet onyx
#

So when you enter play mode, unity resets all the variables because it does a domain reload, so to fix it you either have to generate it again in runtime, or the best solution is save it in a file and load that file in runtime

dapper bobcat
#

Hey! That totally fixed it for me, thanks!!

dapper bobcat
#

So right now I did it super inefficiently to make sure ot was working, and I'm doing it ever time I check.

I'm not super clear on what functions trigger when automatically, so if I wanted to cache the variable when would I want to do it?

I was able to get it checking when I hit the run button, you helped me find a way to check it ever time, but whats the correct middle ground? Start()? OnEnable() as well?

quiet onyx
#

probably in Awake(), but that means you have to ship this code in the executable for it to work

quiet onyx
quiet onyx