Issue Where Components Are Being Added Upon Application Quit

Hey everyone, hope all is going well for you!

This issue confuses the hell out of me, so my apologies if I don’t quite explain things correctly.

The situation:
I have a script that adds colliders to specific bones in a model prefab for more precise hit detection. Up until yesterday, this worked flawlessly. Two components are added, or should be added, at run-time. The first being a script to detect hits and the second being the collider.
After all this happens with each relevant bone, the model should be all nicely laid out with colliders for all important body parts.

The problem: a collider and script pair are not being added as they should be, at the very start of the game. Instead, they seem to be added right at the very end, and stick around after the game has ended. This causes essentially an infinite buildup of component pairs, one set added each time you play the game and end it.

All the code I’ve been working with has not been messed with since it had a face-lift about a week ago. Up until yesterday and now today, it was working flawlessly. If anyone has any clue as to what I could be doing wrong, do let me know, I would appreciate it greatly.

Also I’ll post code as requested, because I don’t even know where to begin with this.

Thanks and take care!

Post the code where you add the components, please include the entire scope of the code including function declaration, and preferably the class it is in (you can leave out any functions/methods unrelated to the adding components).

Sounds like your code is firing during an event/message you don’t expect it to. Like maybe it was moved from ‘OnEnable’ to ‘OnDisable’, or something of the sort (don’t know the architecture of your code).

1 Like

Hey Lordofduct, thanks for the reply!

Here’s the requested code.

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

public class HitboxCreator : MonoBehaviour
{
    //List of bones in the armature of an uncompared skeleton
    List<GameObject> bones = new List<GameObject>();

    bool hasTested = false;

    void Awake()
    {
        CreateTemplate();
    }

    void CreateTemplate()
    {
        //Unrelated code here
    }

    void FindSkeleton()
    {
        //Unrelated code here
      
        CompareSkeletons();
    }

    void CompareSkeletons()
    {
        //Unrelated code here

        CreateHumanoidHitboxes();
    }

    void CreateHumanoidHitboxes()
    {
        BoxCollider temp = new BoxCollider();

        //First thing is to get the root bones of the colliders
        for (int i = 0; i < bones.Count; i++)
        {
            if (bones[i].name == "spine03")
            {
                //Add a box collider component to this bone
                bones[i].AddComponent<BoxCollider>();

                //Add a HitboxMinion script to the bone
                bones[i].AddComponent<HitboxMinion>();

                //Get the newly created box collider component, store it in a temp box collider variable
                temp = bones[i].GetComponent<BoxCollider>();

                //Set up all the appropriate variables for this bone's box collider
                temp.center = new Vector3(-0.10f, 0, 0.05f);
                temp.size = new Vector3(0.73f, 0.58f, 0.41f);

                //Ensure that this collider is not a trigger
                temp.isTrigger = false;
            }
          
            //More code goes here that is the same as the above, just with different bones
        }
    }

    void Update()
    {
        if (!hasTested)
            FindSkeleton();
    }
}

This is attached to an empty gameobject that then takes the prefabs I give it and adds all necessary components.

Thanks!

    void Update()
    {
        if (!hasTested)
            FindSkeleton();
    }

Why is this the way you do this???

Update is now firing every frame despite most likely not needing to do anything.

I’m assuming hasTested gets set true after the first time FindSkeleton does it’s thing (no idea, don’t see where you modify that value), in which case why not call ‘FindSkeleton’ in ‘Start’?

Oh right, forgot to include where I set that to true, apologies!

You raise a valid point, it looks like that particular implementation was being used in a different way previously, and I thoughtlessly ported it over to a more robust system. Thanks!

Fixed that oversight, however the issue still remains, unfortunately.

I suppose my question would be as follows: do I need to add in some functionality that removes all relevant components when the game stops running?

So if the things are sticking around in the scene after you’ve stopped playing, they’re not firing as you quit, they’re firing as you “enter” the editor. I can think of two things causing that to happen:

1: Your script is tagged with ExecuteInEditMode
2: Your code is being fired from OnDestroy

1 Like

Hey Baste, thanks for the reply!

As far as OnDestroy() is concerned, I searched the solution and had no hits.
There were also no hits with that ExecuteInEditMode you mentioned (which is honestly something I had never heard of until now.)

One thing I am noticing is that everything seems to work fine until I try to add any UI components/objects to the prefab that has these scripts executing.
I don’t really see how it could affect things, but then again I’ll be the first to admit I know very little of all the details contained within Unity.

This is odd. Normally adding components in OnDestroy or OnDisable will spit out a bunch of console warnings when you leave play mode.

It might be worth trying to reproduce this in a stripped down project. See if it’s actually a weird engine bug.

Hey BoredMormon, thanks for the reply!

I’ll probably end up doing that tomorrow because I’m still curious as to what the problem is.

However I can say that reverting to a previous version of the project and using a different UI tutorial somehow seems to have resolved the problem. All the hit boxes get created just fine at the beginning of the game cycle, and remove themselves at the end, just as it worked previously.
It’s quite possible I somehow messed something up unintentionally with the first tutorial I followed.
Either way, hopefully it won’t show up again. =-)

Thanks everyone for your responses, and take care!

1 Like