Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Skeebo87 · Mar 23, 2021 at 10:39 PM · instantiatearray of gameobjects

Instantiating array in GameManager upon loading Scene

I have two scenes. One scene is world map that you can navigate with a marker that represents the player. On this world map there are enemy markers that represent groups of enemies you can encounter if their 2D colliders hit. Once these two colliders hit, it will load the second scene, the battle scene.

Before the battle scene is loaded, I first run a method in GameManager that finds all gameObjects that have the EnemyMarker script and store them in an array (GameObject enemyMarkers[]). I also record the locations of each enemy marker as well in GameManager. My issue is that after the battle is over and I load the WorldMap scene again, my method that instantiates all of those enemy markers to their previous transform.location says all items in my array are suddenly null. I use Debug.Log to insure that the objects saved to my array are indeed the objects I intended to save. My GameManager is set to DontDestroyOnLoad as well.

I am still only a few weeks into learning c# and Unity so I am sure that I am missing a concept. My goal is to place those enemy markers right where they were before the battle scene loaded. These instances of enemy markers hold important information that are unique to each marker (i.e. how many enemies are this enemy group) so I don't want to simply instantiate a prefab because those variables will likely change due to the RNG used to determine said variables. Any suggestions? Code provided below:

 public class GameManager : MonoBehaviour
 {
     public static GameManager instance;
     private PlayerMarker pm;
     public Item[] equippedItems = new Item[11];
 
     [Header("Enemy Encounter Info")]
     public int numberOfMeleeEnemies;
     public int numberOfRangedEnemies;
     public string enemySize;
 
     [Header("Save Between Scenes")]
     private GameObject[] enemyMarkers;
     private Vector2[] previousMarkerPositions;
     private Vector2 previousPlayerPosition;
     public bool battled;
     
 
     private void Awake()
     {
         if (instance != null && instance != this)
         {
             Destroy(gameObject);
             return;
         }
 
         instance = this;
         DontDestroyOnLoad(gameObject);
     }
 
     private void Update()
     {
         if(battled)
         {
             LoadScene();
             battled = false;
         }
     }
 
     public void SaveScene()
     {
         EnemyMarker[] eMarkers = FindObjectsOfType<EnemyMarker>();
         Debug.Log("Number of eMarker Objects Found: " + eMarkers.Length);
         enemyMarkers = new GameObject[eMarkers.Length];
         previousMarkerPositions = new Vector2[eMarkers.Length];
         pm = FindObjectOfType<PlayerMarker>();
 
         for (int i = 0; i < enemyMarkers.Length; i++)
         {
             enemyMarkers[i] = eMarkers[i].gameObject;
             previousMarkerPositions[i] = enemyMarkers[i].transform.position;
         }
 
         Debug.Log("Number of enemy markers in array: " + enemyMarkers.Length);
         Debug.Log("Name of first saved enemy: " + enemyMarkers[0]);
         previousPlayerPosition = pm.gameObject.transform.position;
     }
 
     public void LoadScene()
     {
         Invoke("UpdateScene", 0.1f);
     }
 
     private void UpdateScene()
     {
         Time.timeScale = 1;
 
         for (int i = 0; i < enemyMarkers.Length; i++)
         {
             if(enemyMarkers[i] != null)
             {
                 Instantiate(enemyMarkers[i], previousMarkerPositions[i], Quaternion.identity);
                 Debug.Log("Enemy Marker " + i + " instantiated!");
             }
             
         }
         pm = FindObjectOfType<PlayerMarker>();
         pm.gameObject.transform.position = previousPlayerPosition;
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Three Spots For Three Random Objects 1 Answer

How do I acces a specific GameObject within the terrain???? 1 Answer

Checking if object intersects? 1 Answer

Array values changing after gameObject is instantiated from prefab 1 Answer

how to set a GameObject variable to a GameObject[] variable ? - C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges