Don’t worry, I didn’t even start trying to save the resource gathering system state, because… well… detecting a respawned gameObject is not working for some reason, and I’m still trying to figure out why (at least it’s not buggy animations anymore - by far the most ridiculous bug fix to me, comparing how much work I tried before fixing it)
by the way, if it helps in anyway, this is my ‘ResourceRespawner.cs’ script, which is responsible for respawning stuff (and I’m guessing where ‘IsValid()’ also comes into play). To some extent, I also think ‘IsValid()’ also should’ve referred to something here to get this to work:
using System.Collections;
using UnityEngine;
namespace RPG.ResourceManager
{
public class ResourceRespawner : MonoBehaviour
{
[SerializeField] GameObject resourceToRespawn;
[SerializeField] int hideTime;
[SerializeField] int originalQuantity = 10;
private GameObject resourceObject;
private ResourceGathering resourceGathering;
internal bool isRespawning = false;
void Start()
{
// Initialize the resourceGathering component
resourceGathering = GetComponent<ResourceGathering>();
// Spawn the initial tree
SpawnSource();
}
void Update()
{
// Check if the current tree is destroyed and respawn it
if (resourceObject == null && !isRespawning)
{
StartCoroutine(RespawnAfterDelay());
}
}
IEnumerator RespawnAfterDelay()
{
isRespawning = true;
yield return new WaitForSeconds(hideTime);
SpawnSource();
isRespawning = false;
}
private void SpawnSource()
{
// Instantiate a new tree ('Quaternion.identity' = no Rotation assigned - Success):
resourceObject = Instantiate(resourceToRespawn, transform.position, Quaternion.identity);
// Make the current tree a child of this GameObject (the Tree Respawner - Success):
resourceObject.transform.parent = transform;
// Assign a tag to the spawned resource source - NOT NECESSARY THOUGH (Success):
// resourceObject.gameObject.tag = "ResourceTree";
// Access the ResourceGathering script on the instantiated object and set its properties (Success):
ResourceGathering resourceGathering = resourceObject.GetComponent<ResourceGathering>();
// If you have a new source for a specific resource, reset the values for another round of resource gathering:
if (resourceGathering != null)
{
// Reset the quantity left for resource gathering
resourceGathering.quantityLeft = originalQuantity; // Reset the quantity for the next resource instance
resourceGathering.isDestroyed = false; // Reset the IsDestroyed flag
}
}
}
}
My main problem here is, they are respawnables. For some reason, once a Respawnable is dead, this error just becomes a permanent error in the game, and I can’t wrap my head around it (in simpler terms, anytime I press the action map button of resource gathering, this error shows up, whether I’m near a resource or far away from it…):
And when I click on them, this is what I get from the first one:
MissingReferenceException while executing 'performed' callbacks of 'Player/InteractWithResource[/Keyboard/u]'
UnityEngine.InputSystem.LowLevel.NativeInputRuntime/<>c__DisplayClass7_0:<set_onUpdate>b__0 (UnityEngineInternal.Input.NativeInputUpdateType,UnityEngineInternal.Input.NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate (UnityEngineInternal.Input.NativeInputUpdateType,intptr
and the second one:
MissingReferenceException: The object of type 'ResourceGathering' 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.
RPG.Core.RangeFinder`1[T].FindNearestTarget () (at Assets/Project Backup/Scripts/Core/RangeFinder.cs:50)
RPG.States.Player.PlayerFreeLookState.InputReader_HandleResourceGatheringEvent () (at Assets/Project Backup/Scripts/State Machines/Player/PlayerFreeLookState.cs:136)
RPG.InputReading.InputReader.OnInteractWithResource (UnityEngine.InputSystem.InputAction+CallbackContext context) (at Assets/Project Backup/Scripts/Input Controls/InputReader.cs:176)
UnityEngine.InputSystem.Utilities.DelegateHelpers.InvokeCallbacksSafe[TValue] (UnityEngine.InputSystem.Utilities.CallbackArray`1[System.Action`1[TValue]]& callbacks, TValue argument, System.String callbackName, System.Object context) (at Library/PackageCache/com.unity.inputsystem@1.7.0/InputSystem/Utilities/DelegateHelpers.cs:46)
UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)
and since we’re at it, here is my current ‘ResourceGathering.ITarget.IsValid()’ function:
bool ITarget.IsValid()
{
if (!Inventory.GetPlayerInventory().HasSpaceFor(resourceToAccumulate)) return false;
if (skillStore.GetSkillLevel(associatedSkill) < LevelToUnlock()) return false; // place an 'or' ("||")statement later to count for holding a weapon associated to the skill (hatchet for trees, pickaxe for mining, etc), by checking the 'equipLocation.Weapon'
// inject code here that checks if EquipLocation.Weapon has the required axe (level-based check) or not
resourceGatherer.Gather(this);
return true;
}
and there’s no way you can harvest them ever again after that, and it’s driving me nuts, as in WHY can I harvest the first one, but not the second time onwards, although EVERYTHING is exactly the same in terms of settings…?
MORE IMPORTANTLY, WHY CAN’T I HARVEST LITERALLY ANYTHING AT ALL IF ONE RESOURCE IS DESTROYED (I’m not angry, just highlighting an extremely important issue)
At this point in time, I’m convinced that there must be a setting of some sort that tells the RangeFinder code that this item is destroyed, but that doesn’t mean it should permanently stop searching for new ‘ResourceGathering.cs’ scripts (it’s like someone who isn’t over their ex yet, and won’t date other girls)… I think I can copy something from the Pickups regarding that, as it doesn’t seem to have the same problem, just not sure what
Edit: 9 hours later, still can’t figure out what in the world is going on here…