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;
}
}