Search Unity

error on spawner and the destroy object does not work

Discussion in 'Scripting' started by pgood0586, Nov 15, 2022.

Thread Status:
Not open for further replies.
  1. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    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 <c399efc08928422d8828d74a2b19ffba>:0)
    UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at <c399efc08928422d8828d74a2b19ffba>:0)
    UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at <c399efc08928422d8828d74a2b19ffba>: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);
    }
    }

    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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.
     
  3. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    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
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    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.
     
  5. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    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?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    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.
     
  8. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    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.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    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.
     
  10. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    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
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
  12. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    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
     
    Lisole likes this.
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Look at your code.

    Look at the tutorial.

    Make your code EXACTLY PRECISELY like the tutorial.
     
  14. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    And where is the tutorial, because from which I took this idea, there is no difference between my code and his code
     
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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
     
  16. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    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
     
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Which is why the above link tells you how to do it step by step.
     
  18. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    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
     

    Attached Files:

  19. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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.
     
  20. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    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
     
  21. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    If you can't understand this, stop doing what your doing
    1. COPY and PASTE your code here, in CODE TAGS
    2. Tell us where the error is coming (Script, Line, Column) everything, no ones opening random pngs
    3. We'll work from there!
     
  22. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    This is the code, but it did not write to me where the error is or in which line it just writes to me the null error that I sent above


    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);
    }

    }
    }
     
  23. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    You may keep posting the code as many times as you like.

    However, when you are ready and prepared to fix the problem, refer to my FIRST REPLY to you above.

    Nobody can help you until you do the three steps.
     
  24. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    [QUOTE = "Kurt-Dekker ، post: 8596504، member: 225647"] يمكنك الاستمرار في نشر الرمز عدة مرات كما تريد.

    ومع ذلك ، عندما تكون جاهزًا ومستعدًا لإصلاح المشكلة ، ارجع إلى الرد الأول لك أعلاه.

    لا أحد يستطيع مساعدتك حتى تقوم بالخطوات الثلاث. [/ QUOTE]
    أرسلته إليه لأنه سأل أيضًا ، حتى الآن ، لم أتمكن من العثور على الشيء المفقود ،
    And the steps is Finding the missing thing and finding out why it is missing and putting the missing thing, but until now I did not know what the missing thing was or in any line even
     
  25. pgood0586

    pgood0586

    Joined:
    Oct 23, 2022
    Posts:
    46
    [QUOTE = "Kurt-Dekker، post: 8596504، member: 225647"] عندما تكون جاهزًا ومستعدًا لإصلاح المشكلة ، راجع أول رد لك أعلاه. [/ QUOTE]
    هل يمكنك إرسال فيديو بالخطوات وسيكون قريبًا من الكود الخاص بي؟ لأنني حرفيًا لا أجد أي شيء آخر أفعله بخلاف حذف الكود بأكمله وكتابته ، ولا أعتقد أنه سيكون هناك تغيير لأنه لا أعرف مكان الخطأ ، فقد تكون هناك مشكلة في ترجمة شيء ما أو شيء يمكنني اكتشافه في الفيديو And also, should something be added in the script of the spawner to the coin? Maybe this is the missing thing
     
    Last edited: Nov 19, 2022
  26. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    1) You are not providing enough information for anyone to help you. Please go through the information Kurt provided to help find your problem. No one can answer your question because just don't have enough info.
    2) The errors say the object has been destroyed, so it may be another script that is removing it and you aren't doing null checks, but it is impossible to say with the limited information provided.
    3) Before you post questions again, please review the forum rules. USE CODE TAGS, and don't paste images of code. Also post all the relevant information needed.

    This is just going in circles at this point, I am going to close it because you just keep asking the same thing and not giving any additional information. I recommend you go the learn section and start going through tutorials.
     
    MelvMay likes this.
Thread Status:
Not open for further replies.