Search Unity

Variables set by in-editor function set to null at run-time

Discussion in 'Scripting' started by Mr_Admirals, Feb 2, 2019.

  1. Mr_Admirals

    Mr_Admirals

    Joined:
    May 13, 2017
    Posts:
    86
    Okay, so I'm building a snow deformation system, and and there's quite a few settings available for the developer to tweak around with. Previously, you could only see these settings take effect at run-time. To speed up the process of tweaking settings, I switched the code over to doing a function-call attached to a button in the inspector. This is mostly working, but the main issue is that when I press play, a number of variables set by the function are suddenly null.

    Anyone have any insight into this?

    Code (CSharp):
    1.     public void ApplySnow()
    2.     {
    3.         tiles = GameObject.FindGameObjectsWithTag("Tile");
    4.  
    5.         if (tiles.Length == 0)
    6.         {
    7.             Debug.LogWarning("No tiles found in scene");
    8.             return;
    9.         }
    10.  
    11.         if (groundLayer == objectLayer)
    12.         {
    13.             Debug.LogWarning("WARNING: Object Layer and Ground Layer are the same. Snow deformation will not work as intended");
    14.         }
    15.  
    16.         for (int i = 0; i < tiles.Length; i++)
    17.         {
    18.             if(tiles[i].transform.childCount > 0)
    19.             {
    20.                 if(Application.isEditor)
    21.                 {
    22.                     Debug.Log("Destroying Module");
    23.                     DestroyImmediate(tiles[i].transform.GetChild(0).gameObject);
    24.                 }
    25.                 else
    26.                 {
    27.                     Destroy(tiles[i].transform.GetChild(0).gameObject);
    28.                 }
    29.             }
    30.  
    31.             tiles[i].layer = groundLayer;
    32.  
    33.             Bounds tileLocalBounds = tiles[i].GetComponent<MeshFilter>().sharedMesh.bounds;
    34.          
    35.             //Initialize Snow Deformation Module
    36.             //Code here
    37.  
    38.             //Initialize Snow Render Texture Pipeline
    39.             //Code here
    40.  
    41.             //Initialize the two objects
    42.             SRTP.ApplySettings();
    43.             moduleControl.Initialize();
    44.         }
    45.     }
    Code (CSharp):
    1.     public void ApplySettings()
    2.     {
    3.         if(snowMat != null)
    4.         {
    5.             Debug.Log("Destroying snowMat");
    6.             DestroyImmediate(snowMat);
    7.         }
    8.         if(blurMat != null)
    9.         {
    10.             DestroyImmediate(blurMat);
    11.         }
    12.         if(normalizeMat != null)
    13.         {
    14.             DestroyImmediate(normalizeMat);
    15.         }
    16.  
    17.         //Initialize Materials and Render textures here
    18.     }
    19.  
    20.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    21.     {
    22.         if(!active)
    23.         {
    24.             return;
    25.         }
    26.      
    27.         //Says normalizeMat is null here
    28.         Graphics.Blit(groundDepth, depthTexture, normalizeMat);
    29.  
    30.         RenderTexture rt1, rt2;
    31.  
    32.         blurMat.SetTexture("_MyDepthTex", depthTexture);
    33.  
    34.         if (downSampleMode == DownSampleMode.Half)
    35.         {
    36.             rt1 = RenderTexture.GetTemporary(depthTexture.width / 2, depthTexture.height / 2, 24, depthTexture.format);
    37.             rt2 = RenderTexture.GetTemporary(depthTexture.width / 2, depthTexture.height / 2, 24, depthTexture.format);
    38.             Graphics.Blit(depthTexture, rt1, blurMat, 0);
    39.         }
    40.         else if (downSampleMode == DownSampleMode.Quarter)
    41.         {
    42.             rt1 = RenderTexture.GetTemporary(depthTexture.width / 4, depthTexture.height / 4, 24, depthTexture.format);
    43.             rt2 = RenderTexture.GetTemporary(depthTexture.width / 4, depthTexture.height / 4, 24, depthTexture.format);
    44.             Graphics.Blit(depthTexture, rt1, blurMat, 1);
    45.         }
    46.         else
    47.         {
    48.             rt1 = RenderTexture.GetTemporary(depthTexture.width, depthTexture.height, 24, depthTexture.format);
    49.             rt2 = RenderTexture.GetTemporary(depthTexture.width, depthTexture.height, 24, depthTexture.format);
    50.             Graphics.Blit(depthTexture, rt1, blurMat, 0);
    51.         }
    52.  
    53.         for (var i = 0; i < blurIterations; i++)
    54.         {
    55.             blurMat.SetTexture("_MyDepthTex", rt1);
    56.             Graphics.Blit(rt1, rt2, blurMat, 2);
    57.             blurMat.SetTexture("_MyDepthTex", rt2);
    58.             Graphics.Blit(rt2, rt1, blurMat, 3);
    59.         }
    60.  
    61.         blurMat.SetTexture("_MyDepthTex", rt1);
    62.         Graphics.Blit(rt1, blurredDepthTexture, blurMat, 0);
    63.         snowMat.SetTexture("_DispTex", blurredDepthTexture);
    64.  
    65.         RenderTexture.ReleaseTemporary(rt1);
    66.         RenderTexture.ReleaseTemporary(rt2);
    67.     }
    Code (CSharp):
    1.    
    2.     public void Initialize()
    3.     {
    4.         SRTP = transform.GetChild(0).GetComponent<SnowRenderTexturePipeline>();
    5.  
    6.         Bounds meshLocalBounds = transform.parent.GetComponent<MeshFilter>().sharedMesh.bounds;
    7.         Bounds meshWorldBounds = transform.parent.GetComponent<MeshRenderer>().bounds;
    8.         float colliderHeight = SRTP.groundCam.farClipPlane;
    9.         boxCollider = GetComponent<BoxCollider>();
    10.         boxCollider.center = new Vector3(0, meshLocalBounds.center.y - transform.parent.parent.position.y + SRTP.displacement / 2f, 0);
    11.         boxCollider.size = new Vector3(meshLocalBounds.extents.x * 2f, colliderHeight, meshLocalBounds.extents.z * 2f);
    12.  
    13.         SRTP.SetActive(true);
    14.  
    15.         objectCam = transform.GetChild(0).GetComponent<Camera>();
    16.         objectCam.Render();
    17.         groundCam = transform.GetChild(1).GetComponent<Camera>();
    18.         groundCam.Render();
    19.     }
    20.     private void Update()
    21.     {
    22.         if(SRTP == null)
    23.         {
    24.             Debug.Log("How!?");
    25.         }
    26.  
    27.         if(objectCount > 0)
    28.         {
    29.             // Says SRTP is null here
    30.             SRTP.SetActive(true);
    31.             objectCam.enabled = true;
    32.             groundCam.enabled = true;
    33.         }
    34.         else
    35.         {
    36.             SRTP.SetActive(false);
    37.             objectCam.enabled = false;
    38.             groundCam.enabled = false;
    39.         }
    40.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,799
    I think what you need is to explicitly mark those fields as SerializeField so that at the transition between running and editing, Unity handles loading/saving those values you set at editor time.

    It's the usual decorator:

    Code (csharp):
    1. [SerializeField] MyClass YabbaDabbaDoo;