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 zorrotwee · Jan 10, 2017 at 06:23 PM · c#noobpractices

Better code for initiating gameobjects in order

Hi All,

I'm new to Unity en programming and I'm trying to program my first Game. The most important thing for me is learn about how everything works. I'm trying to make a match-3 game, and found some nice tutorials online. My game is working fine but i'm still have some questions about the code en the methods used. Hope you guys can help me out.

To save some memory the game reuses the objects instead of destroying them. In the start we initialise a bank of object and put them in a list tileBank. After we have created the list we shuffle it and place the tiles on the screen using following code:

 for (int r = 0; r < rows;r++){ 
     for (int c = 0;c<cols;c++){
         Vector3 tilePos = new Vector3 (c, r, 0);
        for (int n = 0; n < tileBank.Count; n++) {
         GameObject o = tileBank [n];
             if (!o.activeSelf) {
                 o.transform.position = new Vector3 (tilePos.x, tilePos.y, tilePos.z);
                 o.SetActive (true);
                 tiles [c, r] = new Tile (o, o.name);
                 n = tileBank.Count + 1;
             }
         }
     }
   }
 }

Can I delete the creation of the tilePos Vector en directly code it with the transformation of the tile like this:

  o.transform.position = new Vector3 (c,r,0);

If I understand the code correctly the placement of tiles start everytime with the first tile in the bank and if it's not used check the next. If it's not used, use it en go to the next tile.

I would have coded it like this

 for (int r = 0; r < rows;r++){ 
     for (int c = 0;c<cols;c++){
         GameObject o = tileBank [r*c+c];
         o.transform.position = new Vector3 (c, r, 0);
         o.SetActive (true);
         tiles [c, r] = new Tile (o, o.name);
     }}

My code seem to do the same job. But me being a novice programmer I ask myself. Why is the more complex code better?

Comment
Add comment · Show 2
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 Scoutas · Jan 10, 2017 at 07:48 PM 1
Share

The complex code has some really redundant things written inside. One of those things - putting the vector coordinates inside of a vector. That's really weird.

Vector3 tilePos = new Vector3 (c, r, 0);

o.transform.position = new Vector3 (tilePos.x, tilePos.y, tilePos.z);

is exactly the same thing as:

Vector3 tilePos = new Vector3 (c, r, 0);

o.transform.position = tilePos ;

or even

o.transform.position = new Vector3 (c, r, 0);

It's redundant and unneccecary. I understand why you'd want to place it inside of a variable, for example: if after getting the position, you would want to manipulate it a little, before assigning the position to an object? But used in this context - uneccessary.

The loop that goes through the tileBank is a little weird as well and the way to "optimize" it, would be to find the index of the tile like this col + row * rowWidth. You wouldn't get a tile that is already active this way, so the loop is redundant and can be omitted.

n = tileBank.Count + 1; this line is used to get out of that inner loop, but you can use break as it only stops the loop that you are currently on and would proceed with the loops that are 'higher up'. So yeah, the way you wrote the code is short and to the point, where the more complex code has some redundency.

avatar image zorrotwee Scoutas · Jan 10, 2017 at 08:51 PM 0
Share

Thanks for the quick reply and the clear explanation

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

OnCollisionEnter not working 1 Answer

Alarm lights not changing 1 Answer

Explanation as to how this game mechanic works? 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