0
\$\begingroup\$

I'm trying to make a pickup and drop script by myself and every time the game calls the pickup function, it seemed to cause memory leaks and freezes the game for few seconds. enter image description here enter image description here enter image description here enter image description here 200 -> 130 After those few seconds, the items does get picked up but the fps drops significant and the drop function seems to do fine.
After getting picked up hierarchy

*Attached to the Item holder

using UnityEngine;

public class PickupController : MonoBehaviour
{
    public GameObject itemHolder;

    public bool slotFull;

    private GameObject currentObject;

    private IPickable pickupScript;

    public void PickUp(GameObject pickupObject)
    {

        if (pickupObject == null)
        {
            Debug.LogError("Pickup Object is null");
            return;
        }

        slotFull = true;
        currentObject = pickupObject;
        pickupScript = pickupObject.GetComponentInChildren<IPickable>(); //I'm trying to get the WheelRange.cs script

        Debug.Log(pickupObject.gameObject.name);

        if (pickupScript == null)
        {
            Debug.LogError("pickupScript is null.");
            return;
        }


        currentObject.transform.SetParent(itemHolder.transform);

        pickupScript.OnItemPickup();

        currentObject.transform.localPosition = Vector3.zero;
        currentObject.transform.localRotation = Quaternion.identity;
    }

    public void Drop()
    {
        slotFull = false;
        currentObject.transform.SetParent(null); //Removes the parent
        pickupScript.OnItemDrop();
    }
}

*Attached to WheelRange gameobject

using UnityEngine;

public class WheelRange : MonoBehaviour, IPickable
{
    
    [SerializeField] private Canvas canvas;
    [SerializeField] private BoxCollider boxCollider;
    [SerializeField] private PickupController pickupScript;
    private GameObject parentObject;
    private bool beingUsed;
    private float distToGround;


    void Start()
    {
        beingUsed = false;
        canvas.gameObject.SetActive(false);
        parentObject = this.transform.parent.gameObject; //Gets the actual wheel gameobject
        distToGround = boxCollider.bounds.extents.y;
    }
    public void OnItemPickup()
    {
        beingUsed = true;
        pickupScript.PickUp(parentObject);

        boxCollider.gameObject.SetActive(false);
        canvas.gameObject.SetActive(false);
        
    }

    public void OnItemDrop()
    {
        beingUsed = false;
        boxCollider.gameObject.SetActive(true);
        
    }



    //Rest of these functions probably doesn't apply to the error
    bool IsGrounded()
    {
        return Physics.Raycast(parentObject.transform.position, Vector3.down, distToGround + 0.1f);

    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player") canvas.gameObject.SetActive(true);
    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player") canvas.gameObject.SetActive(false);
    }
}

The interface

public interface IPickable
{
    void OnItemPickup();

    void OnItemDrop();
}
\$\endgroup\$
5
  • \$\begingroup\$ I see 999+ logs, warnings, and errors. Just writing and logging that many strings to the console can itself cause significant overhead. What do you observe if you fix those warnings and errors first, then remove/turn off any unnecessary string logging, then examine the allocations in the profiler? What function shows up as the allocation hot spot? \$\endgroup\$ Commented Aug 19, 2024 at 21:20
  • \$\begingroup\$ Those logs, warnings, and errors are all just those 3 things(Shown in first screenshot) repeated. The warnings seems to go away when I open up unity again, but reappears after Pickup() is called after pressing the icon above the wheel. It seems to be appear every time I click the play button after the first instance unless I reopen unity. \$\endgroup\$ Commented Aug 19, 2024 at 21:57
  • \$\begingroup\$ Not just warnings, I meant all of those logs, warnings, and errors go away when I reopen unity \$\endgroup\$ Commented Aug 19, 2024 at 22:03
  • 2
    \$\begingroup\$ Isnt there a script missing? Nothing calls the IsGrounded and it looks like you have a circular reference since OnItemPickup calls pickupScript.PickUp and in PickUp you call pickupScript.OnItemPickup() \$\endgroup\$ Commented Aug 20, 2024 at 6:10
  • \$\begingroup\$ @Zibelas The IsGrounded function isn't being used yet. I didn't see the circular reference there, and after removing the pickupScript.OnItemPickup() line from the PickupController, everything works fine now. Thank you \$\endgroup\$ Commented Aug 20, 2024 at 17:06

1 Answer 1

2
\$\begingroup\$

Somehow working but freezing the game and/ or using a huge amount of memory is the typical indicator for a memory leak. There are a few easy causes:

  • Allocating memory and forgetting to free it and calling it often (not likely in for your problem)
  • Doing a recursive call and never terminating it (could be but there is no recursion in your code)
  • Doing a circular call

If you check your code, you are calling OnItemPickup. Inside that you are calling the method pickupScript.PickUp. PickUp once again calls pickupScript.OnItemPickup() -> there is your circular call.

Unity terminates that circular call after a while (usually when you run out of memory), at that point the call gets executed. The timeout is first causing your lag (since you need to run first out of memory) and at the same time causing it to work after a while.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.