Your script should either check if it is null or you should not destroy the object.```
It keeps pointing at this line:
```UIController.instance.currentWeaponHolder.material = UIController.instance.matBlueMaterial;```
It's really odd because I'm not destroying the currentWeaponHolder, and it's still there when this error pops up, and it's not destroyed.
Am I missing something here? Thanks!
Here's the whole error:
~~~csharp
MissingReferenceException: The object of type 'Image' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.EventSystems.UIBehaviour.IsActive () (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/UIBehaviour.cs:28)
UnityEngine.UI.Graphic.SetMaterialDirty () (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Graphic.cs:285)
UnityEngine.UI.Graphic.set_material (UnityEngine.Material value) (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Graphic.cs:454)
UnityEngine.UI.Image.set_material (UnityEngine.Material value) (at C:/Program Files/Unity/Hub/Editor/2020.1.15f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Image.cs:743)
PlayerController.SwitchGun () (at Assets/Scripts/PlayerController.cs:514)
PlayerController.RestoreState (System.Object state) (at Assets/Scripts/PlayerController.cs:752)
SaveableEntity.RestoreState (System.Object state) (at Assets/SaveableEntity.cs:36)
SavingSystem.RestoreState (System.Collections.Generic.Dictionary`2[TKey,TValue] state) (at Assets/SavingSystem.cs:118)
SavingSystem.Load (System.String saveFile) (at Assets/SavingSystem.cs:34)
SavingWrapper.LoadWeapons () (at Assets/SavingWrapper.cs:43)
DataSaveLoad.LoadLevelStart () (at Assets/Scripts/System/DataSaveLoad.cs:192)
DataManager.Awake () (at Assets/Scripts/System/DataManager.cs:87)
~~~
Clearly! Find what you’re destroying and find who is trying to keep using it.
The answer is always the same… ALWAYS. It is the single most common error ever.
Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
- also known as: Unassigned Reference Exception
- also known as: Missing Reference Exception
- also known as: Object reference not set to an instance of an object
The basic steps outlined above are:
- Identify what is null
- Identify why it is null
- Fix that.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
https://discussions.unity.com/t/814091/4
Step by step, break it down, find the problem.
This is the simplest explanation of nullreference I’ve ever read. Thank you. I feel like my eyes are open.