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

Editor Scripts: Variables Disappear After Pressing Play

Discussion in 'Editor & General Support' started by SecondDegreePerm, Jan 28, 2020.

  1. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
    Hi,

    I am using an editor script to call some inititialisation dunctions which set various fields, but some of these fields dissappear after pressing the play button. Why is this? I am absolutely certain that these variables are not being removed by my own code, as when I manually set these variables they are not deleted when play mode is activated. It must be something to do with how Unity deals with manual initiation vs initiating variables in editor scripts. All the fields that are initialised have the [SerializeField] tag. My code is as follows:

    Editor Script initialisation
    Code (CSharp):
    1.  
    2.         if (GUILayout.Button("Initialise Map"))
    3.         {
    4.             map.initialiseAllComponents();
    5.         }
    6.  
    Code (CSharp):
    1.  
    2. public void initialiseAllComponents()
    3.     {
    4.         foreach (MapSection s in map) {
    5.             s.initialise();
    6.         }
    7.         if (playerRacer != null) {
    8.             playerRacer.playerRacerStart();
    9.         }
    10.     }
    This then iterates through all the objects I need to be initialised for the scene. There are a lot of things initialised here, so I will not go through them all. Suffice to say most of them are simple variable assignments that provably work in the scene, and then dissappear when the play button is pressed. For some reason some of these variables persist, whilst others do not. There is one case where an object of the same type remains instatiated for one object, but not another. I have read that this may be something to do with fields being serialized, and a solution may be to make a scene dirty? Though I am not sure whether this is the best solution, and I cannot explain why some stay initialised and others do not. Can anyone advise how I can keep these initialisations from being deleted once play is pressed? If anything is unclear or you feel that you need any more information, feel free to ask.

    Thanks in advance
     
  2. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
  3. SecondDegreePerm

    SecondDegreePerm

    Joined:
    Jul 2, 2017
    Posts:
    42
    Can anybody help with this? Is this thread not relevant to these forums? If it isnt I am happy to move it to another forum. If not am I being clear enough with what the issue is? I am happy to clarify if that is the case.
     
  4. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    136