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 /
avatar image
0
Question by GameC12 · Oct 08, 2017 at 04:21 AM · instantiategameobjectstilesforeachstrategy

Instantiating Too Many Objects

I have a turn based strategy game, and it's based on tile movement. I want to put a marker over the tiles the player can move to. I have an array of all the tiles in the map, and then I have a list of the tiles within the move radius. I have a working method for telling and listing which tiles are within the radius, but when I instantiate the markers, it instantiates way too many. Also, I need to do the foreach loop so that the tiles will be placed above each individual one. Any help will be appreciated. Code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MovementController : MonoBehaviour
 {
     public int moveRadius;
 
     public GameObject[] tilesInGame;
 
     public List<GameObject> tilesToMove = new List<GameObject> ();
 
     public GameObject moveTarget;
 
 
     void Start()
     {
         tilesInGame = GameObject.FindGameObjectsWithTag ("Node");
         AssignTilesWithinMove ();
     }
 
     void Update()
     {
         
     }
 
     void AssignTilesWithinMove()
     {
             foreach (GameObject tile in tilesInGame) {
                 if (Vector3.Distance (transform.position, tile.transform.position) < moveRadius) {
                     tilesToMove.Add (tile);
 
                     ShowTarget ();
                 }
             }
     }
 
     public void Move()
     {
         
     }
 
     void ShowTarget()
     {
         Vector3 offset = new Vector3 (0, 1, 0);
 
         foreach (GameObject tile in tilesToMove) {
             GameObject targetMarker = (GameObject)Instantiate (moveTarget, tile.transform.position + offset, Quaternion.Euler(90, 0, 0));
         }
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Lairinus · Oct 08, 2017 at 05:14 AM

You are using a foreach() (ShowTarget) inside of a foreach() (AssignTilesWithinMove). If you have 10 tiles, you are creating 100 markers. If you have 100 tiles, you are creating 10,000.

The bigger problem here is that you are using Instantiate() instead of SetActive(). I would create all of the tiles beforehand, set them as a reference in a script, and then call SetActive() when you need to show them. This is much faster than creating a brand new marker each time.

Comment
Add comment · Show 2 · Share
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
avatar image GameC12 · Oct 08, 2017 at 05:24 PM 0
Share

Okay thanks. I will try that.

avatar image GameC12 GameC12 · Oct 08, 2017 at 05:34 PM 0
Share

Okay it worked, thank you.

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

88 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

Related Questions

Translate a 2D position to a 3D space 1 Answer

Instantiate for loop skips some objects 1 Answer

InvalidCastException: Cannot cast from source type to destination type - can't seem to instantiate objects into an array! 0 Answers

How to Instantiate Objects based on a list of integers 0 Answers

Multiple Fire Points 2 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