So I have these game objects with unique features I’m trying to pass between scenes. Say I’ve defined variables like strength or speed, unique to each object, stored in a script on the object.
What’s the best way to pass that information (ultimately, that object itself) to a new scene?
What I’d attempted thus far was to make a controller script, which stores those other scripts. I’m not at my dev computer from which to post real code, so this is all from memory, but the issue should be clear.
public List<AGenetics> geneticList = new List<AGenetics>();
void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
Store)(
{
GameObject[] objectList = GameObject.FindObjectsWithTag("Creature");
for(int i = 0; i < objectList.Count; i++)
{
geneticList.Add(objectList[i].gameObject.GetComponent<AGenetics>());
}
}
Again, this is written from memory. Syntax may be slightly different (mainly for the DontDestroyOnLoad). This works fine. However, when I move to a new scene, the list becomes depopulated. Keeping the list public allowed me to see this.
I’m not a very good programmer at all, but it seems like it’s “storing by reference.” How can I truly store the scripts in a data structure that keeps their information and not just a reference?
After I get this working I may need assistance with restoring the object in full–with restoring these customized scripts to an object–but I’ll cross that bridge when I get there.
You’re not going to be able to store the scripts, since the scripts get destroyed.
What you’re going to want to do is create tokens of the values for the scripts. Essentially copies of the data.
public class AGenetics : MonoBehaviour
{
public float Health;
public float Speed;
public float Defense;
public object GetStateToken()
{
return new Token() {
Health = this.Health,
Speed = this.Speed,
Defense = this.Speed
};
}
public void RestoreFromStateToken(object token)
{
var t = token as Token;
if(t != null)
{
this.Health = t.Health;
this.Speed = t.Speed;
this.Defense = t.Defense;
}
}
public class Token
{
public float Health;
public float Speed;
public float Defense;
}
}
Then, before you leave a scene, you find all the things you need to persist between scenes and call ‘GetStateToken’ on them storing those objects into a list/array.
Then when the next scene loads, you again find all the objects you must update. And update them accordingly.
Of course… you’r going to need to uniquely identify which object is which for each token. Maybe by the ‘name’ property? That’s up to you to decide.
That was a very good and viable answer given above (me).
However, might I ask or suggest that if you plan to recreate the object entirely, maybe just use DontDestroyOnLoad on any object you’re moving between scenes?
Thanks for the initial answer. It sounds like you’re saying to store the AGenetics script? That script is on the object itself, and…
…I don’t really want to save the object because I need to change out other scripts on the object–remove one, maybe add one (though it would keep the AGenetics script). Additionally, I’m not going to be saving ALL objects when passing between scenes, only ones the user designates.
Is that a bad idea? Would it be simpler to keep the objects (with a simple bool that changes based on if a user picks them or not), and delete or add the necessary scripts then?
Edit: or is a Token a particular type of object, not a general term? I’ll have to take a look at that.
Nah, it’s not a bad idea… I was only adding that “just in case”.
The other solution posted was very good for what you want. It just stores the data in its own class that you can save and use to repopulate the script.