Hello I am having an issue with the scheduler that I cannot seem to debug for the life of me.
I have created a class called PickupSpawner. The function of PickupSpawner is to spawn a pickup at a location using SpawnPickup(), and then when the pickup is picked up or deactivated, it calls ScheduleSpawnPickup(), which schedules SpawnPickup() to trigger after m_SpawnDelay seconds. The code for both methods is below.
Now the weird thing that is happening is that the PickupSpawner works fine in the beginning (it spawns new pickups after the previous one was picked up), but after a while, it just stops spawning pickups completely. Sometimes it stops spawning after 1 minute into the scene, sometimes it takes 30 minutes. However, everytime when I inspect the SpawnPickup, I see the following: the scheduled event is not active, and the m_CurrentPickupObject, which is only modified in SpawnPickup(), and ScheduleSpawnPickup(), is set to null. There are no errors or warnings that appear in the console.
Since m_CurrentPickupObject is null, and Debug.Log("Rescheduled " + gameObject.name") doesn't print out anything to the console, this leads me to believe that the SpawnPickup() action never executed. What I'm wondering is, how is this that even possible? Is there any scenario in which the scheduler doesn't fire a scheduled event?
Note: If it matters, I always set m_SpawnDelay to a positive float like 3f or 15f. I am using Third Party Controller v2.4.4 and Ultimate Inventory System v1.2.4.
Edit: I realized I put this in the wrong forum, it should actually go in UltimateCharacterController > Questions, sorry!
I have created a class called PickupSpawner. The function of PickupSpawner is to spawn a pickup at a location using SpawnPickup(), and then when the pickup is picked up or deactivated, it calls ScheduleSpawnPickup(), which schedules SpawnPickup() to trigger after m_SpawnDelay seconds. The code for both methods is below.
Now the weird thing that is happening is that the PickupSpawner works fine in the beginning (it spawns new pickups after the previous one was picked up), but after a while, it just stops spawning pickups completely. Sometimes it stops spawning after 1 minute into the scene, sometimes it takes 30 minutes. However, everytime when I inspect the SpawnPickup, I see the following: the scheduled event is not active, and the m_CurrentPickupObject, which is only modified in SpawnPickup(), and ScheduleSpawnPickup(), is set to null. There are no errors or warnings that appear in the console.
Since m_CurrentPickupObject is null, and Debug.Log("Rescheduled " + gameObject.name") doesn't print out anything to the console, this leads me to believe that the SpawnPickup() action never executed. What I'm wondering is, how is this that even possible? Is there any scenario in which the scheduler doesn't fire a scheduled event?
Note: If it matters, I always set m_SpawnDelay to a positive float like 3f or 15f. I am using Third Party Controller v2.4.4 and Ultimate Inventory System v1.2.4.
Edit: I realized I put this in the wrong forum, it should actually go in UltimateCharacterController > Questions, sorry!
Code:
/// <summary>
/// Spawns the item pickup.
/// </summary>
public void SpawnPickup()
{
GameObject pickupPrefab = SelectPickupCandidate();
Quaternion rotation = Quaternion.identity;
Vector3 position = Vector3.zero;
if (pickupPrefab == null || !m_SpawnPoint.GetPlacement(pickupPrefab, ref position, ref rotation))
{
Debug.Log("Rescheduled " + gameObject.name);
ScheduleSpawnPickup();
}
else
{
m_CurrentPickupObject = ObjectPool.Instantiate(pickupPrefab, position, rotation);
m_CurrentPickupObject.transform.parent = m_PickupParent;
EventHandler.RegisterEvent(m_CurrentPickupObject, "OnRetractStart", ScheduleSpawnPickup);
EventHandler.RegisterEvent(m_CurrentPickupObject, "OnPickupDespawn", ScheduleSpawnPickup);
}
}
/// <summary>
/// Schedules the spawn of a pickup.
/// </summary>
private void ScheduleSpawnPickup()
{
if (m_CurrentPickupObject != null)
{
EventHandler.UnregisterEvent(m_CurrentPickupObject, "OnRetractStart", ScheduleSpawnPickup);
EventHandler.UnregisterEvent(m_CurrentPickupObject, "OnPickupDespawn", ScheduleSpawnPickup);
// CancelScheduledSpawnPickup();
m_CurrentPickupObject = null;
m_ScheduledSpawnPickup = Scheduler.ScheduleFixed(m_SpawnDelay, () => SpawnPickup());
}
}
Last edited: