error on spawner and the destroy object does not work

hey guys Does anyone know what is the solution to this error and also why Destroy Object is not working

the error

MissingReferenceException: The object of type ‘GameObject’ 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.Object.Internal_InstantiateSingle (UnityEngine.Object data, UnityEngine.Vector3 pos, UnityEngine.Quaternion rot) (at :0)
UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at :0)
UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at :0)
spawner.Update () (at Assets/spawner.cs:26)

spawner code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spawner : MonoBehaviour
{
public GameObject[ ] spawns;
float timeBtwSpawns = 1;

void Start()
{

}

void Update()
{
timeBtwSpawns -=Time.deltaTime;

if (timeBtwSpawns <= 0)
{
timeBtwSpawns = Random.Range(0.5f, 1.5f);
Vector2 spawnposition = new Vector2();
spawnposition.x = Random.Range(-2.5f, 2.5f);
spawnposition.y = -6;
Instantiate(spawns[Random.Range(0, spawns.Length)], spawnposition, Quaternion.identity);
}

}
}

the coin code for destroy

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class get_coin : MonoBehaviour
{
public Rigidbody2D rb;
void start () {

}

void Update() {
rb.velocity = Vector2.up * 300 * Time.deltaTime;
}

void OnTriggetEnter2D(Collider2D other)

{
if (gameObject.transform.position.y > 7)

if(other.gameObject.tag == “player”)
{
Destroy(gameObject);
}
}

}

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

You may edit your post above.

Looks like you’re destroying something, then later trying to use it. Don’t do that.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

If you’re completely lost above, perhaps you need to review your tutorial-doing process.

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Finally, when you have errors, don’t post here… just go fix your errors! Here’s how:

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Well, now I’m supposed to have made a spawner, ball, lines, and coins. Destroy the ball order for it to be destroyed by the lines. It works, but the Destroy command to destroy the coin with the ball does not work. This error appears when the game starts. Can I know what to do now and in which script exactly And I also put lines and currencies in the spawner Until they are reset about every two seconds

Don’t post code as a wall of plain text, please edit your post to use code-tags . Also, please don’t use every tag you can find on your posts; it has nothing to do with tilemap, 2d physics, performance, documentation etc.

I’ll move your post to the Scripting forum, your problem is not related to any 2D feature.

I didn’t understand what you mean it’s a 2d game so I posted the problem here Do I have to do something else?

Yes. Get busy with tutorials. Use the two-step process I listed above.

The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

This assumes you have at least written and studied some code and have run into some kind of issue.

If you haven’t even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

If you just want someone to do it for you, you need go to one of these places:

https://forum.unity.com/forums/commercial-job-offering.49/

https://forum.unity.com/forums/non-commercial-collaboration.17/

https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons

The 2D forum isn’t for any issue using any of the features in the Unity engine. There are dedicated forums for Scripting, Animation, Cinemachine, Physics, Audio/Video etc.

Your post is you typing stuff wrong, it’s a general scripting issue. You didn’t ask about any 2D features such as Sprites, Tilemap, 2D Physics, SpriteShape etc. This is why I moved the post so all is good. Well, apart from the fact that you didn’t edit your post to use code-tags. It’s just a wall of plain text, difficult to read and refer to.

Sorry, but I almost did not understand whether I should ask somewhere about what is my mistake or what should I do exactly because now the order to destroy is to the position and the order to destroy when the ball touches the coin does not work and so far I have not found a solution and I do not know where the error is and I also asked people They have experience in these things and they did not know what was wrong. Can I know what I should do now to find out where the error is and what the solution is? Sorry for the inconvenience.

TBH I doubt it, you’re not reading what is being put such as the request a few times to use code-tags above (it’d take you 30 seconds to do that). You’re still expecting devs here to be able to read that wall of plain text (count the line that the error is on somehow) and expecting them to somehow debug your project in their head; most devs will ignore posts like this.

You are deleting a GameObject then you end up doing the same again on the same GameObject. Why? No idea, all we can see is a wall of plain text that might not even be relevant to the why. I understand you are stuck here but the truth is that if you don’t put the effort in, neither will anyone else.

[quote=“MelvMay, post:9, topic: 900225, username:MelvMay”]
أتفهم أنك عالق هنا ولكن الحقيقة هي أنك إذا لم تبذل الجهد فلن يفعل أي شخص آخر.
[/quote] The matter is not like this, of course, because before I asked someone had tried to modify the codes to know where the error was, and also I did not know, and even after I asked, I was trying to find the error as well, and I did not find it, and it is not even written for me in Unity, where is the error in fact There is no written error in the original, so I know why Destroy does not work, and I certainly did not find that there was an error, and I came here immediately to ask without trying to fix it And I may have been answered regarding this question, but I did not understand exactly, sorry if this happened

This is your 4th reply and you’ve still not spent a few seconds to edit your original post to use code-tags. Honestly, I don’t understand your reply.

Note, this will never be called either:

void OnTriggetEnter2D(Collider2D other)

It’s not “OnTriggetEnter2D”.

Hello, sorry for the late reply because I am Arabic, and maybe this is also the reason why I did not understand what you mean well and there is a time difference, I have another question that may be strange because I am new to Unity and there are things I do not understand yet. If I change OnTriggetEnter2D, do I change the script afterwards I only change OnTriggetEnter2D because I tried OnTriggerExit2D, OnTriggerStay2D and OnCollisionEnter2D although I don’t know what it has to do with it, but I tried it and nothing changed. Can I know what I should change yet? Sorry to bother you because I still don’t understand some of these things

1 Like

Look at your code.

Look at the tutorial.

Make your code EXACTLY PRECISELY like the tutorial.

And where is the tutorial, because from which I took this idea, there is no difference between my code and his code

Did you fix the nullref yet? That’s a runtime error, NOT in the code.

Did you fix it or are you still wasting time before doing step #1?

I’ll post it AGAIN for you:

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Well, how can I know what is missing and where is it? Sorry I asked so much but this is my first time dealing with these errors

Which is why the above link tells you how to do it step by step.

Now either I did not understand or I do not know where the error is exactly, and I think I read everything that was written and did not understand what was missing

This is the code that comes to me when I click on the error, and this is the error that appears to me more than once when I click start. Can we start step by step from here because I do not want to add anything else that gives me another error and I do not know what errors I have
Now which line should I start with?
And I think the error is from the first line 18


Everything I wrote in my first post is what you need to fix this.

The first step is for YOU to find out what variable it is.

Until you do this step, everything is wasted time.

Perhaps there is simply a language barrier here.

You may wish to find a better way to translate the help above.

I know that all this is a waste of time, but I have literally been trying these codes for two weeks, and I don’t know where the error is, and I don’t know How to fix it, and I have read almost everything in the link, and it was almost all about finding the empty or missing thing, and putting the missing thing in its place, but I do not know what is missing first in the code that I pictured above