Heisenbug with stack overflow

Hi all!
I have a problem with attached script. I dont understend this behavior and i can’t debug this.
In project I have terrain with collider, and object(box). On start, box have position(0,1,0). When my player have collision with this box, i destroy it box with timeout(Destroy(gameObject, 2.0f)). Before destroy I make instantiate. It’s work fine, but i need check, that distance from box to terrain always eqaul 1 meter. This is make by recursive function void HeightCorrection(). And here i have “heisenbug”, because sometime it’s work fine, sometime i have stackoverflow error in recursive function. Debug.Log(“…”) not work for me - on stackoverflow Unity Editor fall.
Please, help me - I read docs, forums, but I can’t search resolve for this fail. Sorry for my bad english.

Below is a simplified script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VFX;
using UnityEditor;

public class LutScore : MonoBehaviour
{
    public int score = 1;
    public int failedCounter = 0;

    public GameObject lutPrefab;

    private GameObject cloneInstance;
    private Renderer rend;
    private bool kludge = true;
    private Ray lutRayNegative, lutRayPositive;

    // Start is called before the first frame update
    void Awake()
    {
        rend = GetComponent<Renderer>();
        if (rend == null) { Debug.LogError("rend fail"); }

        // kludge, needed because we Instantiate an object
        kludge = true;
        score = 1;
    }

    void Start()
    {
        HeightCorrection();
    }

    // Update is called once per frame
    void Update()
    {
        if (score == 0 && kludge) {
            kludge = false;
            CreateInscance();
            rend.enabled = false;
            Destroy(gameObject, 2.0f);
        }
    }

    // Function for transform object position on 1 meter above terrain
    void HeightCorrection()
    {
        RaycastHit lutHitN, lutHitP;
        bool neg_f = false, pos_f = false;
        float rayDistance = 1.0f;

        // Check distance to terrain
        lutRayNegative = new Ray(gameObject.transform.position, gameObject.transform.up * (-1.0f));
        lutRayPositive = new Ray(gameObject.transform.position, gameObject.transform.up);

        if (Physics.Raycast(lutRayNegative, out lutHitN))
        {
            if (lutHitN.collider.gameObject.CompareTag("Terrain"))
            {
                if (!Mathf.Approximately(lutHitN.distance, rayDistance))
                {
                    gameObject.transform.position += new Vector3(0.0f, (rayDistance - lutHitN.distance), 0.0f);
                    neg_f = true;
                }
            }
        }
        if (Physics.Raycast(lutRayPositive, out lutHitP))
        {
            if (lutHitP.collider.gameObject.CompareTag("Terrain"))
            {
                if (!Mathf.Approximately(lutHitP.distance, rayDistance))
                {
                    gameObject.transform.position += new Vector3(0.0f, (rayDistance + lutHitP.distance), 0.0f);
                    pos_f = true;
                }
            }
        }

        //Collision not detected - Terrain collider inside object collider
        if (neg_f == false && pos_f == false)
        {
            failedCounter += 1;
            gameObject.transform.position += Vector3.up;

            //here is stackoverflow
            HeightCorrection();
        }
    }

    // TODO: Maybe problem is here
    void CreateInscance()
    {
        cloneInstance = Instantiate(lutPrefab, gameObject.transform.position, gameObject.transform.rotation) as GameObject;
        //right function
        //cloneInstance.transform.position += new Vector3(Random.Range(-6.0f, 6.0f), Random.Range(-6.0f, 6.0f), Random.Range(-6.0f, 6.0f));
        //function for debug
        cloneInstance.transform.position += new Vector3(1.0f, Random.Range(-6.0f, 6.0f), 0.0f);
    }
}

6078906–659574–LutScore.cs (4.62 KB)

Please use code tags: Using code tags properly

How to report problems productively in the Unity3D forums:

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

Help us to help you.

1 Like

I see you have a “failedCounter” variable that increments whenever you make the recursive call. To help you debug, what if you bail out with a simple “return” and log an error message if failedCounter reaches an unreasonably high number, like 200? (not sure how many iterations you are expecting)

That will stop the StackOverflow and let you see your Debug.Log statements.

1 Like