I really need some help here. I have a singleton script with database functions that are made into lists of stats, vitals, skills, etc. I want to display the contents of those lists in another script( in this case it’s an HUD that shows your stats and name at the top.)
I’ve tried everything I can think of to no avail!
I’ve tried calling the list to be made from the HUD script and the database script. --null reference
I’ve tried creating the variables on the database script, even inside the list function --was able to get and return the very first stat and have it display, but the second gives me an index out of range error.
public List<Vitals> Vitals;
public List<Skills> Skills;
public List<Stats> Stats;
void Awake()
{
SQLiteInit();
Vitals=new List<Vitals>();
Skills= new List<Skills>();
Stats=new List<Stats>();
}
static PlayerDatabaseManager instance;
public static PlayerDatabaseManager GetInstance()
{
return instance;
}
void Start()
{
LoadStats();
Constitution=Stats[0].Constitution;
Stamina=Stats[1].Stamina;
}
public int GetCon()
{
return Constitution;
}
public int GetStam()
{
return Stamina;
}
In the HUD script:
void Start ()
{
PlayerDatabaseManager.GetInstance().GetCon();
PlayerDatabaseManager.GetInstance().GetStam();
}
I thought I was on to something there, but alas.
So, what is the best way to either return elements of a list or the entire list to another function that needs to use them?
I hope someone has an answer out there and can clearly walk me through.
I’ve watched countless tutorials and cannot find one that addresses this situation.
Thanks!
Where do you set the PlayerDatabaseManager instance? Also helps if you paste the error in the quesiton.
if (instance == null) instance = this;
Let’s assume that you have a script in GameObject A with name ScriptA, and another script in another GameObject B with name ScriptB. ScriptB wants to get access to a public method in ScriptA. You need to do this in ScriptB:
GameObject.Find(“GameObject_A”).GetComponent< ScriptA >().public_method()
You should consider creating a struct to contain your data and creating one List of type YourStruct. It seems you have three custom Types anyway.
Passing Lists is exactly the same as passing other variables. Just make sure your container has been initalised before trying to populate it with data.
Just make public List<Vitals> Vitals;
static
public static List<Vitals> vitals;
Then access it
PlayerDatabaseManager.vitals;
If there’s only going to be one of them, this is perfect.
Or you some kind of instance method :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerDatabaseManager : MonoBehaviour {
public static PlayerDatabaseManager instance;
public List<int> vitals = new List<int>();
void Awake()
{
if(instance == null) instance = this;
vitals.Add(32);
vitals.Add(21);
}
}
using UnityEngine;
using System.Collections;
public class CallDatabase : MonoBehaviour {
void Start ()
{
Debug.Log(PlayerDatabaseManager.instance.vitals[0]);
Debug.Log(PlayerDatabaseManager.instance.vitals[1]);
}
}
I simply use GetComponent a lot to access the particular class containing my one list. I tend to use static at a minimum. Sometimes I cache the scripts if I use it a lot and sometimes I make an instant call, so the memory is freed up afterwards.