How to update variables in array between scenes

I have things like spawn points for enemies already on every scene and i want to use only one script with findGameObjectsWithTag that will automatically update them between scenes.

I would imagine you would want to run a check on Awake() for whether your variables are !null and if so, you want to Destroy() those objects to make room for the new objects. If null you would DontDestroyOnLoad() and use FindGameObjectsWithTag() to fill them on Start().

public class DontDestroySpawns : MonoBehavior
{
public static DontDestroySpawns yourSpawnVar;
void Awake()
{
if(yourSpawnVar != null)
{
Destroy(this.gameObject);
}
else
{
DontDestroyOnLoad(this.gameObject);
}

}
}

// On another script
public GameObject[] theList;
void Start()
{
DontDestroySpawns.yourSpawnVar.theList = GameObject.FindGameObjectsWithTag("YourTagHere");
}

Of course this is more of a Singleton approach if you want to look into that to see if it’s a good fit for you. Also note you wouldn’t actually do the code exactly like this since you have a list or array to store them in. You would want to iterate through the list with a foreach() and send each var to the other script. I just threw this together rather quickly as an example.

That is not exactly what i’m looking for, i have spawn points as variables in script and i want them to be updated upon loading a new scene if it is possible

Not really understanding what you are trying to do here. I am just assuming you have stored these variables in a list or array, correct? You want to use these same variables to store each new level’s spawn points, right? I think I can help you once I get a better understanding of what it is you’re trying to do.

variables are in array and i want to use these same variables to store each level’s spawn points and i do not know how to do that!

Ok, that’s easy. You have your variables stored into the array but you need new information each new level at the start. I don’t know what you named your array, so I will call it storedVars[ ] and the new spawn zones(points) I will suggest making a new array to store them, let’s call that array newVars[ ]

You only need to iterate through newVars and store each element into storedVars according to the index.

The code to do this would look something like this:

// assume your storedVars array is up here and that storedVars
// is already popuated
private GameObject[] newVars;
private int index;
void Start()
{
index = 0; // Insure it starts at 0 every new scene
newVars = GameObject.FindGameObjectsWithTag("YourTag");
foreach(GameObject newVar in newVars)
{
// Here you will inject each new element into storedVars
storedVars[index] = newVar;
index++;
}
}

I think this may initialize the variables inside storedVars rather than replace them? Either way, it may be the same thing, no?

I couldn’t get it to work. But I found the onLevelWasLoaded() class and it almost working.

btw I’m still learning and started learning arrays not so long ago, so I don’t quite understand them

Did you have tags on your spawn game objects? Surely there must be a reason the code didn’t work.

Arrays and list are the same in a lot of ways yet have different functionality. For example, you can use Sort(), Add(), Remove(), etc… I’m not 100% positive but I believe Arrays start at element 0, where List<>() start at element 1, which is why if you’re doing a random range based upon the length, you need to subtract 1. (List.Count - 1) as opposed to Array.Length to get the proper amount.

Look into System.Collections. Which includes Arrays, List, Enumerables, and Dictionaries. I don’t fully understand Enumerables but there’s a lot of cool stuff you can do with them!