tippington
New member
I'm working on a project where a player can open chests, much like in Fortnite. When a chest is opened, it spawns a number of pickup objects and then destroys itself. A manager script will spawn new chests every 30 seconds in any empty chest spawn spots.
My issue is, after opening a chest and going to open a different one, I'm getting the following error in the Unity console:
Is there something I'm doing wrong to cause this error to appear? Here's the code for my chest object:
Thanks in advance.
My issue is, after opening a chest and going to open a different one, I'm getting the following error in the Unity console:
Code:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Opsive.UltimateCharacterController.Character.Abilities.DetectObjectAbilityBase.CanStartAbility () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/Abilities/DetectObjectAbilityBase.cs:137)
Opsive.UltimateCharacterController.Character.Abilities.Interact.CanStartAbility () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/Abilities/Interact.cs:99)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.TryStartAbility (Opsive.UltimateCharacterController.Character.Abilities.Ability ability, System.Boolean ignorePriority, System.Boolean ignoreCanStartCheck) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1070)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.TryStartAbility (Opsive.UltimateCharacterController.Character.Abilities.Ability ability, System.Boolean ignorePriority) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1052)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.TryStartAbility (Opsive.UltimateCharacterController.Character.Abilities.Ability ability) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:1041)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotionHandler.TryStartAbility (Opsive.UltimateCharacterController.Character.Abilities.Ability ability) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotionHandler.cs:231)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotionHandler.UpdateAbilityInput (Opsive.UltimateCharacterController.Character.Abilities.Ability[] abilities) (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotionHandler.cs:128)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotionHandler.UpdateAbilityInput () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotionHandler.cs:98)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotionHandler.Update () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotionHandler.cs:73)
Is there something I'm doing wrong to cause this error to appear? Here's the code for my chest object:
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Opsive.UltimateCharacterController.Traits;
public class ChestBehavior : MonoBehaviour, IInteractableTarget
{
[Header("Config")]
public int totalItems;
public List<GameObject> itemList;
[HideInInspector] public ChestSpawnBehavior thisSpawn;
private bool isOpen;
private const float openDur = 0.32f;
private const float dieDelay = 0.24f;
public bool CanInteract(GameObject obj)
{
return !isOpen;
}
public void Interact(GameObject obj)
{
Open();
}
private void Open()
{
if (isOpen) { return; }
isOpen = true;
StartCoroutine(_Open());
}
private IEnumerator _Open()
{
Debug.Log("opening chest");
// TODO: Play open animation
yield return new WaitForSeconds(openDur);
List<GameObject> itemsToSpawn = new List<GameObject>();
for (int i = 0; i < totalItems; i++)
{
var thisItem = itemList[Random.Range(0, itemList.Count)];
itemsToSpawn.Add(thisItem);
if (i == totalItems - 1) { SpawnItems(itemsToSpawn); }
}
yield return new WaitForSeconds(dieDelay);
Die();
}
private void SpawnItems(List<GameObject> itemsToSpawn)
{
for (int i = 0; i < itemsToSpawn.Count; i++)
{
Debug.Log("spawning item with index " + i.ToString());
Instantiate(itemsToSpawn[i], transform.position, Quaternion.identity);
// TODO: Add burst animation to items
}
}
private void Die()
{
thisSpawn.ChestDespawned();
Destroy(gameObject);
}
}
Thanks in advance.