- Home /
Missing Reference Exception problem,Junior Programmer course : Missing Reference Exception problem
so I have a problem with this lesson I've done everything as it says but there is an error when i try to shoot prefabs and destroy does anyone have a solution for this the error is : Missing Reference Exception: The object of type 'Game Object' 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.
Thank you all guys for a fast response and help I solve the problem All works perfect now Solution was that I needed to put my projectile game object to a prefab folder
Answer by evskii · Nov 08, 2021 at 03:25 PM
From what the console is saying, you are still trying the access the projectile that has been destroyed. I imagine what is happening is the projectile GameObject exists in the scene and is being referenced in a script, but when it destroys because it is out of bounds, the reference is destroyed. What you want to do is make a prefab of the projectile [Prefabs are created by dragging the GameObject into your project folder] and reference the prefab rather than the GameObject in the scene, this way it can create as many of the prefabs as it wants without having to worry about them being destroyed!
Answer by unity_ka6jgzfPPmtNCw · Nov 08, 2021 at 05:04 PM
When I see this happen, it's usually because I've [SerializeField] GameObject and referenced an object from within the scene that gets deleted before you're accessing/instantiating the GameObject.
Make a prefab and referenece the SerializeField from the prefab.
Answer by A_Lego · Nov 08, 2021 at 12:37 PM
We don't really have much information to go off of, so could you post the actual error itself? But as the error says, your trying to access something that is already destroyed. So check if another script already destroys that object. It might also be that you are referencing the destroyed object somewhere else. Like for example, if you shoot a bullet and it then hits a wall and gets destroyed, but you continue to try to update its position in another script even though it already hit a wall and got destroyed.