0
\$\begingroup\$

I have a script which initialises a bunch of child gameObject(s). I wanted all of this to happen outside of the game, so I created an inspector script which has a button which triggers the method which does all the work. The problem is that when something is initialised it's global variables aren't saved for runtime this even goes so far that when I call a method outside of the one that the inspector script calls it forgets the parameters that were passed in (so they appear as null).

I've tried adding the [Serializable] attribute to the scripts but it doesn't change anything. What do I have to do?

\$\endgroup\$
  • \$\begingroup\$ Can you save instance variables on some entity in the scene instead? Globals are not a good fit for this case, because the things you're pointing to only exist in one scene, so they become meaningless if any other scene is loaded. \$\endgroup\$ – DMGregory May 3 '16 at 1:52
  • \$\begingroup\$ Well I need the variables to be saved in the scene for each object and persist for a couple of sessions. They don't need to be reused outside of the scene that there in, just save what i do to them in an editor script. \$\endgroup\$ – user3797758 May 3 '16 at 13:10
  • \$\begingroup\$ It sounds like you should describe more of your use case — are these being used between editor sessions? If so, saving them to a configuration file or ScriptableObject asset in your project may be suitable. \$\endgroup\$ – DMGregory May 3 '16 at 13:12
  • \$\begingroup\$ yes between editor sessions. There are a few thousand objects being created that need to be modified before they can be used in game. I have a script which does all of that but the variables that i need unity to save get cleared before the editor script code even finishes. \$\endgroup\$ – user3797758 May 3 '16 at 13:18
  • \$\begingroup\$ It works if i put [Serializable]above each variable that i need to save but I don't want them to be visible in the inspector... I guess that i don't have to expose it in the inspector script. \$\endgroup\$ – user3797758 May 3 '16 at 13:23
0
\$\begingroup\$

Have a look at ScriptableObject . This'll allow you to persist this type of information over multiple sessions.

\$\endgroup\$
  • \$\begingroup\$ Is there a way to do something similar but still use monoBehaviour \$\endgroup\$ – user3797758 May 3 '16 at 13:06
0
\$\begingroup\$

Maybe your problem is due to your are using a prefab to have all enclosed, so once you click on this prefab in your project folder the values will be reset to its default values. The solution is dont use a prefab, put your script in a empty game object.

Cheers!

\$\endgroup\$
  • \$\begingroup\$ That's not a solution as even this way it still won't save the variables that I change in the editor. \$\endgroup\$ – user3797758 May 3 '16 at 13:05
0
\$\begingroup\$

So I solved the issue by making all the private global variables in the GameObjects [Serializeable, HideInInspector] This way when my editor script changes the thousands of game objects each save the state that they are given and when the scene in played those states are assigned to the GameObjects which work just like if I did all the calculations at the start of the scene.

As for the parameters being forgotten when a method was called from outside the main method the inspector was calling, I just made it a delegate inside the bigger method. The Method in question was only ever being called once from that method so removing it didn't make a difference.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.