Hello,
I’ve read a few threads about this, but can’t wrap my mind around it…
I have a class ‘Web’ attached to a GameObject. This class has 2 fields I want to expose in the Editor:
[SerializeField]
private string _clientId;
[SerializeField]
private string _projectId;
Since these fields should exist only once, it’d feel logic to make them static. But you apparently can’t expose static fields in the Editor. I’ve read that maybe the workaround is to use a singleton, but I’m not familiar with that and I’m obviously doing it wrong.
This is what I do:
public class Web : MonoBehaviour
{
[SerializeField]
private string _clientId;
[SerializeField]
private string _projectId;
private string clientId, projectId;
private static Web _instance;
private Web()
{
clientId = _clientId;
projectId = _projectId;
}
public string GetClientId()
{
Debug.Log("clientId is " + _instance._clientId);
return _instance._clientId;
}
public string GetProjectId()
{
return _instance._projectId;
}
public static Web GetInstance()
{
return _instance;
}
void Start()
{
_instance = new Web();
}
}
But the debugger shows me that doing that gives me a ‘null’ value for ‘_instance’.
-
Why is that?
-
What would be the correct way to achieve what I want?
-
Is it ‘ok’ (i.e. good practice) to use singletons in scripts/classes that are attached to GameObject? (I have to say I’m a bit confused between the ‘_instance’ and the object obtained via ‘GameObject.GetComponent’…)
-
Should I forget about this idea of using singleton and simply accept that I’ll need to access _clientId and _projectId via something like
GameObject.Find("MyGameObject").GetComponent<Web>()._clientId;
(would that be the best practice) ?
Thanks a lot!!
Arnaud