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

Changing scripts while running creates NullReferenceException

Discussion in 'Scripting' started by BoxOfChoclates, May 12, 2020.

  1. BoxOfChoclates

    BoxOfChoclates

    Joined:
    May 7, 2020
    Posts:
    2
    Making a change to a script in the codebase while unity is still running the project creates NullReferenceExceptions with my events. This error does not appear when initial running, only when changes are made to anything that makes the project recompile.

    Why would the recompile make the GameEvents.current equal null?

    upload_2020-5-12_17-5-50.png

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class GameEvents : MonoBehaviour
    7. {
    8.     public static GameEvents current;
    9.  
    10.     void Awake()
    11.     {
    12.         current = this;
    13.     }
    14.  
    15.     public event Action<RaycastHit> OnManaCast;
    16.     public void ManaCast(RaycastHit hit)
    17.     {
    18.         if (OnManaCast != null)
    19.         {
    20.             OnManaCast(hit);
    21.         }
    22.     }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Valve.VR;
    5.  
    6. public class LaserPointer : MonoBehaviour
    7. {
    8.     public SteamVR_Input_Sources handType;
    9.     public SteamVR_Action_Boolean teleportAction;
    10.  
    11.     public GameObject laserPrefab; // 1
    12.     private GameObject laser; // 2
    13.     private Transform laserTransform; // 3
    14.  
    15.     private Vector3 hitPoint; // 4
    16.     private RaycastHit hit;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         // 1
    22.         laser = Instantiate(laserPrefab,transform);
    23.         // 2
    24.         laserTransform = laser.transform;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         // 1
    31.         if (teleportAction.GetState(handType))
    32.         {
    33.             // 2
    34.             if (Physics.Raycast(transform.position, transform.forward, out hit, 100))
    35.             {
    36.                 hitPoint = hit.point;
    37.                 ShowLaser(hit);
    38.             }
    39.         }
    40.         else // 3
    41.         {
    42.             laser.SetActive(false);
    43.         }
    44.     }
    45.  
    46.     private void ShowLaser(RaycastHit hit)
    47.     {
    48.         // 1
    49.         laser.SetActive(true);
    50.         // 2
    51.         laserTransform.position = Vector3.Lerp(transform.position, hitPoint, .5f);
    52.         // 3
    53.         laserTransform.LookAt(hitPoint);
    54.         // 4
    55.         laserTransform.localScale = new Vector3(laserTransform.localScale.x,
    56.                                                 laserTransform.localScale.y,
    57.                                                 hit.distance);
    58.         Debug.Log(GameEvents.current);
    59.         GameEvents.current.ManaCast(hit);
    60.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Unfortunately that's just how the cookie crumbles. When you recompile C# code, the DLLs contained in your project's Library/ScriptAssemblies are deleted and recreated, then they are reloaded and relinked with the underlying scene and assets on disk. Anything that does not map to a static disk-stored value (such as a prefab setting or a scriptable object setting) will be reset to whatever its default is (null, zero, etc.)
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    If you want, you can configure Unity to not recompile your code until you stop running the game.

    This prevents you from making code changes "live"...but in my experience, trying to do so usually screws up the game beyond recovery anyway.
     
  4. BoxOfChoclates

    BoxOfChoclates

    Joined:
    May 7, 2020
    Posts:
    2
    Thanks for the responses. I guess I can stop my hunt for the answer now that there is no good solution.
     
    sfilo likes this.