• 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 OldLadyNevermore · Aug 12, 2013 at 04:00 AM · performanceparentload

Big performance hit with assigning parent

For some reason, when I enable this line in my code, it causes a BIG performance hit when I start the scene. BIG as in the time to start goes from a few seconds to a couple of minutes.


this.transform.parent = hexGrid.transform;

'this' refers to an instance of my Hex class; this line is from the Initialize method of this class (which essentially just instantiates the hexes variables).


    public void Initialize(HexGrid hexGrid, Vector3 position, int column, int row) {
    //TODO it may be more efficient to derive x and z from columon and row (or vice versa) than to derive them.
        this.hexGrid = hexGrid;
        this.column = column;
        this.row = row;
        this.transform.position = this.transform.TransformDirection(position);
        this.transform.parent = hexGrid.transform;
    }

You will see a reference to HexGrid; HexGrid is the 'shephard' class of hexes, it creates the hexes and allows me to map x,z to a particular hex, etc.

Here is the HexGrid bit of code that creates a Hex and then calls the Initialize:

y = myTerrain.SampleHeight(new Vector3(newHexPosition.position.x, 0, newHexPosition.position.z)); GameObject hexGameObject = (GameObject)(Instantiate(hexPrefab, new Vector3(0,0,0), Quaternion.identity)) as GameObject; Hex newHex = hexGameObject.GetComponent (); newHex.Initialize(this, new Vector3(newHexPosition.position.x, y, newHexPosition.position.z), columnCounter, rowCounter); hexes[columnCounter, rowCounter] = newHex;

                     //Move newHexPosition to the right one hex
                     newHexPosition.Translate(hexWidth, 0, 0);

There is nothing complicated about either class, and that code does work (the hexes instantiate as a child of the parent), but just takes a long time.

Is there a better way to do this?

Comment
Add comment · Show 5
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 Benproductions1 · Aug 12, 2013 at 04:05 AM 0
Share

Are you sure that specific line is causing the "lag" or it it what that line causes?

avatar image Paulius-Liekis · Aug 12, 2013 at 06:17 AM 0
Share

$$anonymous$$aybe you have $$anonymous$$eshColliders and scales on parent objects?

avatar image OldLadyNevermore · Aug 12, 2013 at 07:31 PM 0
Share

I am pretty sure it is that line that is causing the lag: removing that one line resolves the issue (but, of course by hexes are then ophans).

As far as the parent object goes, the parent of these hexes is the hexgrid, which has only a transform and a script. The parent of the hexgrid is the terrain itself.

avatar image Benproductions1 · Aug 13, 2013 at 12:03 AM 0
Share

Just to clear this up, how large is this "hexPrefab"?

avatar image OldLadyNevermore · Aug 13, 2013 at 06:12 PM 0
Share

I am not sure what is meant by 'large'. It is just a script with a simple cube object. It's not even a true 'hex' with six sides: it is just a point in space; I call it a hex because each of them is equidistant from six others, as if you drew a hex grid by recording the center of each hex ins$$anonymous$$d of the lines between them.

I tried to paste the code here, but there were too many characters. I remove the helper methods that are used by hexes to access their neighbors, and here is what is left.

using UnityEngine; using System.Collections; using System.Collections.Generic; using System;

public class Hex : $$anonymous$$onoBehaviour {

 //GameObject go;
 //Transform transform;
 private HexGrid hexGrid;    
 private int column; //The hex's column in the grid, moving one hex to the right increases the column by one
 private int row; //The hex's row in the grid, moving one hex NW increased the hexes row
 
 //List<Hex> neighbors; //Contains all adjacent hexes
 List<Hex> addedNeighbors;
 List<Hex> removedNeighbors;
 
 public bool debug = false;
 
 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     renderer.enabled = debug;
 }
 


 
 public void Initialize(HexGrid hexGrid, Vector3 position, int column, int row) {
 //TODO it may be more efficient to derive x and z from columon and row (or vice versa) than to derive them.
     this.hexGrid = hexGrid;
     this.column = column;
     this.row = row;
     this.transform.position = this.transform.TransformDirection(position);
     //this.transform.parent = hexGrid.transform;
     
     
 }
 

...truncated the getter and setter methods

 //Derives the gameworld coordinates based upon the
 //  hexes column and row
 
 //Derives the hex's column and row based
 //  upon it's x,z coordinates
 public void deriveIndices() {
     //TODO does casting to int break the math
     column = (int)(transform.position.x / hexGrid.HexSize);
     //TODO does this cast break the math?
     row = (int)(transform.position.z / hexGrid.HexSize * .75);
 }
 

 

 
 public bool hasNeighbor(int index) {
 switch (index) {
         case 0: 
             if ((column < hexGrid.Hexes.GetLength(0)-1) && (row < hexGrid.Hexes.GetLength(1)-1)) {
                 return hexGrid.Hexes[column+1,row+1] != null;
             }
             break;

...truncated code for other neighbors... } //todo what about addedNeighbors? return false; }

...truncated code for other neighbors

 //Gets all neighbors, both natural (actually adjacent) and artificial
 //  (connected with adjancies).
 List<Hex> getNeighbors() {
     List<Hex> tempNeighbors = new List<Hex>();
     tempNeighbors.AddRange(getNaturalNeighbors());
     tempNeighbors.AddRange(getAddedNeighbors());
     return tempNeighbors;
 }

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

All project as child of one gameObject is a good idea? 1 Answer

How to do performance/load testing for WebGL games which hosted onto AWS server,How to do Load testing for WebGL games hosted onto AWS server 0 Answers

Loading map is slow 1 Answer

Fast, easy serialization library for Unity? 0 Answers

Serialized variable vs Resources.Load() 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