- Home /
I have an error, script should check for null or not destroy game objects
Hi everyone, I am getting a error message when going from my first scene to my second scene and back to my first scene. If I load my second scene and go to my first scene, there is no error.
The error message reads as follows:
The object of type 'FieldBackground' 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.
My code for the field background is as follows:
using UnityEngine; using UnityEngine.UI; using System.Collections;
[RequireComponent (typeof (Image))] public class FieldBackground : MonoBehaviour {
Image image;
public static Sprite background;
void Awake () {
image = GetComponent<Image>();
UIAssistant.onScreenResize += LoadBackground;
}
void OnEnable() {
LoadBackground();
}
void LoadBackground() {
if (background != null)
image.sprite = background;
Texture2D texture = image.sprite.texture;
float texture_ratio = 1f * texture.width / texture.height;
float screen_ratio = 1f * Screen.width / Screen.height;
RectTransform rect = transform as RectTransform;
if (texture_ratio > screen_ratio) {
rect.offsetMin = new Vector2(-600, 0);
rect.offsetMax = new Vector2(600, 0);
} else {
rect.offsetMin = new Vector2(0, -600);
rect.offsetMax = new Vector2(0, 600);
}
}
}
Before switching to the scenes, there was no error and now there is, but how do you check to see if this background is null or how do you keep the game objects from being destroyed?
Answer by bunnynsnake · Apr 17, 2018 at 06:28 PM
I have had to deal with those. There are a few youtube videos that cover fixing null exception errors, check them out https://www.youtube.com/watch?v=UYziBhvVIcs https://www.youtube.com/watch?v=Y8buHhKcjRc https://www.youtube.com/watch?v=7vOWjCdt2xc best of luck, i know errors can be annoying, but they can be fixed
Answer by shadowpuppet · Apr 17, 2018 at 06:35 PM
so the gameObject that has this script is being destroyed? It looks like your trying to reference that script but it is gone. You can put a donotdestroyonload in there to prevent that gameObject with script from being destroyed. But I don't know what the script is on and you may not want it to get passed from scene to scene
Thanks for the answer. I have tried to use dontdestroyonload (this.gameObject); and dontdestroyonload (gameObject); but they seem not to help. How would I go about preventing the gameObjects from loading into another scene?
so, you are in scene 1 to start and all is fine, you go to scene 2 where that gameObject is destroyed so when you go back to scene 1 the error message pops up? Hmmm. I don't know. $$anonymous$$aybe....if the gameObject with the script was a prefab, then another script in scene 1 says to instantiate in the Start() function? So each time you go to scene 1 that script will put that gameObject with the fieldBackground script on it. I think you would also then need to make that public static sprite a prefab as well. But.....when you go back to scene 1 other gameObjects are there from before when the scene reloads so why isn't that one. Is something else destroying it? Because it seems like it should reload along with everything else that loaded when you were first in scene 1. It isn't something you need in scene 2 you just want it when you go back to scene1, right. Seems like it should be there unless something else is destroying it. like if(IWentToScene2 ==true) destroy that gameObject with the script on it