Unity freezing??

i was creating a void to add objects to an inventory however i got the error StackOverflowException. i only got the error then it just froze? can anyone help + is there any neater way of doing it?
Thanks

void AddToInventory()
{

    Inventory inventoryLoc = inventory.GetComponent<Inventory>();
    ObjectStats objectStats  = Cast.collider.gameObject.GetComponent<ObjectStats>();

    int notAvalible = 0;
    int i = 0;

    print("Test");

    if (i != 36)
    {

        print("Pass");

        if (inventoryLoc.inventorySpace*.Avalible == false)*

{
Debug.Log(i);
notAvalible += 1;
Image logo = inventorylocation.transform.GetChild(i).Find(“ImageLayer”).GetComponent();
logo.sprite = objectStats.icon;
inventoryLoc.inventorySpace*.Avalible = true;*
inventoryLoc.inventorySpace*.Name = objectStats.name;*
inventoryLoc.inventorySpace*.Icon = objectStats.icon;*
}
else
{
if (i == 36)
{
AddToInventory();
}
else
{
AddToInventory();
i += 1;
}
}
}

}
The Whole Script
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class Player : MonoBehaviour {
//VARIBLES
private RaycastHit Cast;
private bool ObjectsInGame;
public string PickupKey = “f”;
public string OpenInv = “e”;
public GameObject inventory;
public GameObject inventorylocation;
private float hunger, thirst, heath;
float hungerDecrease, thirstDecrease, healthDecrease;
float decrease = .1f;
float decrease2 = .2f;
float decrase3 = .3f;
public Text hunderTxt;
public Text thirstTxt;
public Text HealthTxt;
public GameObject HealthBar;
public GameObject ThirstBar;
public GameObject HungerBar;
private bool Starving;
private bool Dehydrated;
public bool InvOpen = false;
//private bool staiminaloss = RigidbodyFirstPersonController.mRunning;
//FUNCTIONS
void Start()
{
Inventory inventoryLoc = inventory.GetComponent();
ObjectStats objectStats = Cast.collider.gameObject.GetComponent();
heath = 70;
hunger = 40;
thirst = 80;
}
public void Update()
{
int HealthInt = (int)heath;
int HungerInt = (int)hunger;
int ThirstInt = (int)thirst;
hunderTxt.text = HungerInt.ToString();
thirstTxt.text = ThirstInt.ToString();
HealthTxt.text = HealthInt.ToString();
var HealthBarSize = HealthBar.transform as RectTransform;
var HungerBarSize = HungerBar.transform as RectTransform;
var ThirstBarSize = ThirstBar.transform as RectTransform;
HealthBarSize.sizeDelta = new Vector2(HealthInt * 2, HealthBarSize.sizeDelta.y);
HungerBarSize.sizeDelta = new Vector2(HungerInt * 2, HungerBarSize.sizeDelta.y);
ThirstBarSize.sizeDelta = new Vector2(ThirstInt * 2, ThirstBarSize.sizeDelta.y);
HungerAndThirst();
if (heath <= 0)
Die();
if (Input.GetKeyDown(OpenInv))
{
if (InvOpen == true)
{
inventory.SetActive(false);
InvOpen = false;
return;
}
else if (InvOpen == false)
{
inventory.SetActive(true);
InvOpen = true;

}
}
if (GameObject.FindGameObjectsWithTag(“Object”).Length > 0)
{
ObjectsInGame = true;
}
else
{
ObjectsInGame = false;
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (ObjectsInGame)
{
if (Physics.Raycast(ray))
{
if (Physics.Raycast(ray, out Cast))
{
if (Cast.collider.tag == “Object”)
{
if (Input.GetKeyDown(PickupKey))
{
PickupObject();
}
}
}
}
}
else
{
return;
}
}
void PickupObject()
{
Destroy(Cast.collider.gameObject);
AddToInventory();
}
void AddToInventory()
{
Inventory inventoryLoc = inventory.GetComponent();
ObjectStats objectStats = Cast.collider.gameObject.GetComponent();
int notAvalible = 0;
int i = 0;
print(“Test”);
if (i != 36)
{
print(“Pass”);
if (inventoryLoc.inventorySpace*.Avalible == false)*
{
Debug.Log(i);
notAvalible += 1;
Image logo = inventorylocation.transform.GetChild(i).Find(“ImageLayer”).GetComponent();
logo.sprite = objectStats.icon;
inventoryLoc.inventorySpace*.Avalible = true;*
inventoryLoc.inventorySpace*.Name = objectStats.name;*
inventoryLoc.inventorySpace*.Icon = objectStats.icon;*
}
else
{
if (i == 36)
{
AddToInventory();
}
else
{
AddToInventory();
i += 1;
}
}
}

}
public void HungerAndThirst()
{
int HungerInt = (int)hunger;
int ThirstInt = (int)thirst;
if (gameObject.GetComponent().Running)
{
hunger -= decrase3 * Time.deltaTime;
}
else
{
hunger -= decrease * Time.deltaTime;
}
thirst -= decrease2 * Time.deltaTime;
if(HungerInt <= 80 && Starving == true)
{
if (hunger <= 0)
{
Starving = true;
print(“Starve”);
heathDecrease();
}
print(“Hunger”);
heathDecrease();
}
if (thirst <= 25 && !Dehydrated)
{
if (ThirstInt == 0)
{
Dehydrated = true;
print(“Dehydrate”);
}
heathDecrease();
}
}
void heathDecrease()
{
heath -= decrase3 * Time.deltaTime;
}
public void Die()
{
Debug.Log(“Player Died”);
hungerDecrease = 0;
healthDecrease = 0;
thirstDecrease = 0;
}
}

You have infinite recursion in AddToInventory(), so you reach maximum stack size (that’s what StackOverflowException means).

More specifically when the expression if (inventoryLoc.inventorySpace_.Avalible == false) is false (so Available is true), you recursively call AddToInventory() on both branches. You increase i in the hope that it will reach 36, but i is a local variable to each call of AddToInventory(), so in the recursive call it will be 0 again, and it will again call AddToInventory, forever (or until you run out of stack)._
Proposed solution is to make your i variable either a member variable, or (better) pass it to the AddToInventory() function as parameter.
In general, when Unity freezes on execution of new code, it is usually an infinite loop, either with for / while loops or with a recursion of some sort.

Thanks, I think one of the main cause was i was calling the AddToInventory before add to i so it looped.