Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Objects are not deleted for an unknown reason

Discussion in 'Scripting' started by DZZc0rd, May 24, 2023.

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

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    Hello, I have a problem. I created a function to remove many objects, but it doesn't do the job.

    I tried adding a delay for "Destroy" function.

    No logs or errors while executing the function.

    Here is code:
    Code (CSharp):
    1. public void ResetPieces()
    2. {
    3.     for (int i = 0; i < _piecesObj.childCount - 3; i++)
    4.         Destroy(_piecesObj.GetChild(i + 3).gameObject);
    5. }
     
    Last edited: May 24, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    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, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    You may edit your post above.

    If you have no idea what is going on, we have even LESS of an idea, so you must FIX THAT FIRST. Here's how:

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all <----- start here
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    109

    My guess is that whenever you destroy a child, the number of children is changing so this logic won't work.
    I don't completely understand your loop because you start from 0 until X-3, and you're removing the index i+3.

    Just Destroy the last child each time or the first child each time, and do it for X times.
     
  4. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    Nah, doesn't work
     
  5. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    105
    Are you saying the function isn't being called at all? (Did you use Debug.Log before the for loop to verify this?)
     
  6. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    No, function is being called
     
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Hopefully _piecesObj is of type Transform.
    It could be that you're targeting the wrong object.
    Test your assumptions.

    And make your function look like this
    Code (csharp):
    1. public void ResetPieces() {
    2.     for(int i = _piecesObj.childCount - 1; i >= 3; i--)
    3.         Destroy(_piecesObj.GetChild(i).gameObject);
    4. }
     
  8. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    It doesn't work either, but I think the problem is in Unity. I'm calling this function in another function and Unity just can't keep up, but when I returning function this function is doing it correctly
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This is the surest tell that you simply haven't yet understood what your code is doing.

    This leads you to conclude that your code is perfect and that instead the problem must lie within one of the worlds most-used game engines.

    I still suggest you go back and find out exactly what you are Destroy()ing so you can fix it to Destroy() what you intend.

    This is also not a thing. Everything in Unity happens one thing at a time. There's no "keeping up." That's not a thing.

    It's not like a bunch of hamsters on wheels that have to keep up.

    It's ONE hamster as far as scripting is concerned.
     
    lordofduct and orionsyndrome like this.
  10. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    I know, what this function is doing. This function is must delete objects from _piecesObj
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    I think you're misunderstanding what Kurt is saying.

    ... I just ran your code in my own project, and it did exactly what I expected it to do. It deleted all the children of _piecesObj except for the first 3.

    Thing is... is that what you want? Is _piecesObj pointing at the correct target? Does _piecesObj have the children in question?

    Have you tried printing/logging anything?

    Try running this:

    Code (csharp):
    1. Debug.Log($"Name:{_piecesObj.name} - ChildCount:{_piecesObj.childCount}");
    2. for (int i = 0; i < _piecesObj.childCount - 3; i++)
    3. {
    4.     Debug.Log($"Destroying: {_piecesObj.GetChild(i + 3).gameObject.name}");
    5.     Destroy(_piecesObj.GetChild(i + 3).gameObject);
    6. }
    Do you get outputs? Are they outputs you're expecting?

    ...

    We don't know you, we don't know what you're thinking, we don't know what you think this is supposed to do, we don't know what your project actually looks like.

    All we have is this code snippet. And this code snippet is technically functional code. So... if you want more help beyond that... you need to give us the information we don't have.

    And Kurt is telling you how you can get more information.
     
  12. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    I have a while loop after the ResetPieces function. If I return the function before that loop, then everything works, if not, then it does not work.
     
  13. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    . . .

    OK, I tried to help.
     
    Bunny83 likes this.
  14. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Maybe we should ask for things instead, I think we have an all-knowing oracle here.
    Look, when I'm going to get rich? In fact, scratch that, will I ever go on a vacation?
    Is there gonna be peace on Earth? What will happen to the poor military-industrial complex?
     
    Brathnann and lordofduct like this.
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I find it fascinating to see how long people will resist doing the steps that will fix their problem.

    I remember one guy who held out for a WEEK refusing to do Step #1 of the NullReferenceException checklist.

    And of course the moment he did it, his problem was obviously and instantly solved, just like this one could be.

    Humans are fascinating!
     
  16. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    I looked now, function is works correctly, but by some reason objects are not destroyes in _piecesObj variable
     
    Last edited: May 24, 2023
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Then they're not the droids you think they are. You may have attempted to Destroy something else.

    Are you perhaps trying to destroy your prefabs rather than the instances you put in your scene?

    Use Debug.Log() and find out.
     
  18. DZZc0rd

    DZZc0rd

    Joined:
    Sep 4, 2021
    Posts:
    58
    Is it normal that in the inspector, after removing the _piecesObj child elements, they remain in the code?
     
  19. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    What does that even mean?
     
    Bunny83 and orionsyndrome like this.
  20. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
  21. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,005
    I might be paranoid here, but for some time now I'm starting to suspect that corporations are training their AI on the internet forums ;)
     
    Bunny83, lordofduct and Kurt-Dekker like this.
  22. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Sure they do. But I'm guessing it's not by letting it actively participate?
     
  23. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,005
    I was thinking exactly about such an activity.
     
    Kurt-Dekker likes this.
  24. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    Aw man... I feel bad, like we're piling on at this point.

    @DZZc0rd - if you're looking for assistance, we need context to the posts you're making.

    Reviewing your post history shows a pattern of vague messages and posts in the end where you basically say "I figured it out, I won't say what it is though, but it's the result of my inattentiveness."

    It's a shame you haven't been able to get suitable responses to help you in your posts, but it's not due to a lack of trying on our side. We just need more context to what you're working on.

    You'll get out of us as much as you put in.

    Hopefully you can do that, at which point I'm more than happy to assist, but until then... I'm going to leave it at that so as to avoid derailing this thread any further.
     
    ensiferum888 and MaskedMouse like this.
  25. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,955
    Closing as this is pointless.
    Please read the rules before posting again. Post all relevant details and ask CLEAR and CONCISE questions. This is a support site, not some sort silly puzzle/guessing game.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.