Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by valentin56610 · Apr 04, 2020 at 11:57 AM · instantiateinstantiate prefabtilesmany

Instantiating 90.000 gameObjects (very simple) takes forever and freeze unity

Hi! So I am making a WWII strategy hex game, and I am making a map of Europe (300x300) and when loading or even creating the map, unity will freeze and it is taking around 3 minutes to create the map. I don't understand how it can be SO long to create just 90.000 simple tiles. Also, it does not even really depend on the hardware because I made my brother generate a map of the same size with his high-end PC and it took the same amount of time. I want to add that the script TileGO that every tile has, doesn't have ANY Start() method or even an Update(). And I would like to add, that I have like 1FPS in game when looking at the 90 000 tiles, is that normal ? when I come closer, it is totally fine. Here is my code :

 GameObject newTile;
 
         for (int x = 0; x < GameData.GetInstance().map.SizeX; x++)
         {
             for (int y = 0; y < GameData.GetInstance().map.SizeY; y++)
             {
                 // LOADING EXISTING MAP
                 if (MapEditorMenu.isLoadedMapEditor)
                 {
                     // Check what version of the tile needs to be instantiated
                     if (GameData.GetInstance().map.TilesTable[x, y].IsWinterTile)
                         season = "Winter/";
                     else
                         season = "Summer/";
 
                     // instantiate the prefab
                     newTile = Instantiate(Resources.Load("Prefabs/Tiles/" + season + GameData.GetInstance().map.TilesTable[x, y].Name) as GameObject);
 
                     // change its material, because in game i added 2D light, but i don't want to bother with it in th editor for performances reasons
                     newTile.GetComponent<SpriteRenderer>().material = BaseTileMaterial;
 
                     //change the GO's name to its coordinates to recover it faster
                     newTile.name = x + " " + y.ToString();
 
                     // add the tileEditorGO component
                     newTile.AddComponent<TileEditorGO>();
 
                     // add the tile in unity to my mapData used for saving
                     newTile.GetComponent<TileEditorGO>().tile = GameData.GetInstance().map.TilesTable[x, y];
 
                     if (GameData.GetInstance().map.TilesTable[x, y].IsVictoryPoint)
                     {
                         // Caching the victory points to reuse later
                         listOfAllVps.Add(newTile.GetComponent<TileEditorGO>());
 
                         // set the city name 
                         if (GameData.GetInstance().map.TilesTable[x, y].CityName != null)
                         {
                             GameObject prefab = Resources.Load<GameObject>("CityNames/BaseCityNameHolder");
                             GameObject cityNameGO = Instantiate(prefab);
 
                             cityNameGO.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("CityNames/" + newTile.GetComponent<TileEditorGO>().tile.CityName);
                             cityNameGO.name = GameData.GetInstance().map.TilesTable[x, y].CityName;
                             cityNameGO.transform.SetParent(newTile.transform);
                             cityNameGO.transform.position = new Vector3(newTile.transform.position.x, newTile.transform.position.y - 2.3f, newTile.transform.position.z - 1.1f);
                         }
 
                         // Add the owner's flag (Germany, USSR ...)
                         selectedPlacedTile = newTile;
                         foreach (Player player in GameData.GetInstance().listOfPlayers)
                             if (GameData.GetInstance().map.TilesTable[x, y].Owner == player.Name)
                                 SetflagForVP(player.Country);
                     }
 
                     // Destroy the original component, because this is the component I use for 
                     Destroy(newTile.GetComponent<TileGO>());
 
                     // spawning the ground unit on the air unit if there is one
                     if (GameData.GetInstance().map.TilesTable[x, y].AirUnit != null)
                         SpawnTheUnit(GameData.GetInstance().map.TilesTable[x, y].AirUnit, newTile);
 
                     // spawning the ground unit on the tile if there is one
                     if (GameData.GetInstance().map.TilesTable[x, y].GroundUnit != null)
                         SpawnTheUnit(GameData.GetInstance().map.TilesTable[x, y].GroundUnit, newTile);
                 }
                 // CREATING A NEW MAP :
                 else
                 {
                     newTile = Instantiate(baseEditorTileSummer) as GameObject;
 
                     newTile.name = x + " " + y.ToString();
                     newTile.GetComponent<TileEditorGO>().tile = new Tile();
                     GameData.GetInstance().map.TilesTable[x, y] = newTile.GetComponent<TileEditorGO>().tile;
                     GameData.GetInstance().map.TilesTable[x, y].Name = "Plain";
                 }
 
                 // assign the tile's parent
                 newTile.transform.SetParent(parent.transform);
 
                 // place the tile on the map
                 if (x % 2 != 0)
                     newTile.transform.position = new Vector2(x * 4.4f, y * 5.1f);
                 else
                     newTile.transform.position = new Vector2(x * 4.4f, y * 5.1f + 2.55f);
             }
         }
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

225 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 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

Invisible Prefab spawning at location of Player before being instantiated 1 Answer

Get a Insantiate particle system follow a GameObject 1 Answer

Collision Detection not working 4 Answers

How to use Animator.Rebind ??? 0 Answers

How to Change the Variables of an Instantiated Object? 0 Answers


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