c# - Show dialog only once, not after level restart -
i have own dialog system gameobject collider. after triggering collider, canvas
text
component should show up. far, works. now, want make happen once. here how work: start level trigger showing dialog. game pretty hardcore player die. when player dies, call application.loadlevel(application.loadedlevel);
(so restart level)
if use in scrip collider
private static bool firsttimetext = true; void ontriggerenter2d(collider2d coll) { if (coll.tag == "player" && firsttimetext) { getcomponent<boxcollider2d>().enabled = false; firsttimetext = false; } }
everything works charm. except if make copy of gameobject, static variable in script "handle" every instantion of object have firsttimetext on false after trigger dialog first time. can use once.
so there sollution making trigger run once , not reset after application.loadlevel
?
also doesn't work
void awake() { dontdestroyonload(transform.gameobject); }
static variables globally identical - therefore if need have multiple gameobjects exhibiting behavior, they'll need hook different static values in way.
one way have key, , keeping static list of "keys" displayed. recommendation hashset
- thus
private static hashset<string> firsttimeset = new hashset<string>(); public string singlerunkey; void ontriggerenter2d(collider2d coll) { if (coll.tag == "player" && firsttimeset.add(singlerunkey)) { getcomponent<boxcollider2d>().enabled = false; } }
note .add
returns true
if item wasn't in collection (and adds it), false
if (and not add it, because no duplicates). have assign singlerunkey
unique value per object via inspector, , each 1 run once
Comments
Post a Comment