When I try to pass in the duration variable as an argument to the wait function, or even call the wait function itself in the blackSnowEvent function, I get the "An object reference is required for the non-static field, method, or property ''" Error. I'm not sure what to do. (I call the blackSnowEvent function in another script (PlayerCollision.cs))
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
private static bool snowActivated = false;
public int duration = 5;
// This is also a singleton just like AudioManger
#region Singleton
private static GameManager instance;
public static GameManager Instance
{
get
{
return instance;
}
}
void Start()
{
AudioManager.Instance.Play(SoundType.BackgroundTheme);
}
IEnumerator wait(int arg){
yield return new WaitForSeconds(arg);
}
public static void blackSnowEvent(ParticleSystem particleSystem, GameObject snowTrigger){
if(snowActivated == false){
snowActivated = true;
snowTrigger.SetActive(false);
particleSystem.Play();
StartCoroutine(wait(duration));
particleSystem.Stop();
snowActivated = false;
}
}
#endregion
}
CodePudding user response:
You are calling a non-static method StartCoroutine(wait(duration))
in a static method blackSnoEvent()
, which throws an error. You either have to make your wait()
method static or make blackSnowEvent()
non-static. If you make blackSnowEvent()
non-static, you access it from the other script using GameManager.Instance.blackSnowEvent()
Recommended:
Make all our fields and methods in your script non-static. If you want to call a method in this script, you access it using GameManager.Instance.SomeMethod()
given SomeMethod()
is a public method.