Search Unity

How to update variables in array between scenes

Discussion in '2D' started by Loud_Lasagna, Dec 1, 2019.

  1. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    21
    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.
     
    Last edited: Dec 5, 2019
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    776
    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().

    Code (CSharp):
    1. public class DontDestroySpawns : MonoBehavior
    2. {
    3. public static DontDestroySpawns yourSpawnVar;
    4. void Awake()
    5. {
    6. if(yourSpawnVar != null)
    7. {
    8. Destroy(this.gameObject);
    9. }
    10. else
    11. {
    12. DontDestroyOnLoad(this.gameObject);
    13. }
    14.  
    15. }
    16. }
    17.  
    18. // On another script
    19. public GameObject[] theList;
    20. void Start()
    21. {
    22. DontDestroySpawns.yourSpawnVar.theList = GameObject.FindGameObjectsWithTag("YourTagHere");
    23. }
    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.
     
    Last edited: Dec 1, 2019
  3. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    21
    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
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    776
    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.
     
  5. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    21
    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!
     
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    776
    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:

    Code (CSharp):
    1. // assume your storedVars array is up here and that storedVars
    2. // is already popuated
    3. private GameObject[] newVars;
    4. private int index;
    5. void Start()
    6. {
    7. index = 0; // Insure it starts at 0 every new scene
    8. newVars = GameObject.FindGameObjectsWithTag("YourTag");
    9. foreach(GameObject newVar in newVars)
    10. {
    11. // Here you will inject each new element into storedVars
    12. storedVars[index] = newVar;
    13. index++;
    14. }
    15. }
    I think this may initialize the variables inside storedVars rather than replace them? Either way, it may be the same thing, no?
     
    Last edited: Dec 3, 2019
  7. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    21
    I couldn't get it to work. But I found the onLevelWasLoaded() class and it almost working.
     
    Last edited: Dec 3, 2019
  8. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    21
    btw I'm still learning and started learning arrays not so long ago, so I don't quite understand them
     
  9. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    776
    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!