Hi folks,
I’m new to Unity and programming in general and recently learned about scriptable objects. I tried to implement one to track the time of day inside my game, but I can’t figure out how to update the time just by using methods inside the scriptable object. I could do that via a MonoBehaviour, but imo that would kinda conflict with the purpose of a scriptable object.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "_World_", menuName = "ScriptableObjects/World")]
public class _World_ : ScriptableObject
{
// ---------------------------- time of day ------------------------------------
private DateTime daytime;
[SerializeField] private bool updateDaytime;
[SerializeField] private string daytimestring;
// --- private
private void OnEnable()
{
Debug.Log("OnEnable");
updateDaytime = true;
daytimestring = "";
}
public IEnumerator NewDayTime()
{
Debug.Log("NewDayTime()");
if (updateDaytime)
{
daytime = DateTime.Now;
Debug.Log(daytime.ToString());
daytimestring = daytime.ToString();
yield return new WaitForSeconds(1);
NewDayTime();
}
}
In my approach the public IEnumerator NewDayTime()
gets called from an MonoBehaviour, but it still just works once, and the method won’t get called again.