Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mitti2000 · Apr 23, 2013 at 04:02 PM · errorupdatefunctionspeed

update() too fast?

Hi there

I'm a beginner with Unity programming and currently trying to create a funvtional Tetris clone. So far, everything I wanted works. I'm working right now on the script that should delete the full lines and move the ones above downwards. So here are the scripts I have for that. The function checkLines() is called out of the update() function:

  //check, if there is a full Line
 function checkLines(){
     var line : int;
     for (var a=19;a>=0;a--){
         for(var b=0;b<10;b++){
             if(board[b,a]==1) line++;
             }
         if (line>=10){
             deleteLines(a);
             moveLines(a);
             }
         line=0;
         }
     }

 //delete the full line
 function deleteLines(a:int){
     for(var X=0;X<10;X++){
         if(blocksLevel[X,a]){
             Destroy(blocksLevel[X,a].gameObject);
             board[X,a]=0;
             }
         }
     }

     //move lines above the deleted
     function moveLines(a:int){
         for(var Y=a+1;Y<20;Y++){
             for(var X=0;X<10;X++){
                 if(blocksLevel[X,Y]){
                     blocksLevel[X,Y].transform.position.y -=2.2;
                     blocksLevel[X,Y].name = "block"+X+a;
                     board[X,Y]=0;
                     board[X,a]=1;
                     }
                 }
             }
         }

This script works exactly as I want it to work as long there is just one single line to delete. As soon as there are 2 to 4 lines, the results are kinda random meaning that sometimes not all blocks get deleted or not all blocks are moved down one unit.

To me it looks like that unity is "jumping" over some lines of code because the update function is called to soon. Is that possible? Is there a problem with my code? What is causing my problem?

I'm be really thankful if you could help me

Thanks mitti2000

Edit: I changed the script to a version without GameObject.Find calls as suggested by Yokimato

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

2 Replies

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

Answer by mitti2000 · Apr 26, 2013 at 09:34 AM

Ok, in the meantime I found the solution myself. Here is my script now:

 //check, if there is a full Line
 function checkLines(){
     var line : int;
     for (var a=19;a>=0;a--){
         for(var b=0;b<10;b++){
             if(blocksLevel[b,a]) line++;
             }
         if (line>=10){
             deleteLines(a);
             moveLines(a);
             }
         line=0;
         }
     }
 
 //delete the full line
 function deleteLines(a:int){
     for(var X=0;X<10;X++){
         if(blocksLevel[X,a]){
             Destroy(blocksLevel[X,a].gameObject);
             }
         }
     }
 
 //move lines above the deleted
 function moveLines(a:int){
     var b= a+1;
     for(var Y=b;Y<20;Y++){
         for(var X=0;X<10;X++){
             if(blocksLevel[X,Y]){
                 blocksLevel[X,Y-1]=blocksLevel[X,Y];
                 blocksLevel[X,Y-1].transform.position.y -=2.2;
                 blocksLevel[X,Y-1].name = "blockcopy"+X+(Y-1);
                 blocksLevel[X,Y]=null;
                 }
             }
         }
     }

I did 2 main things to my prior scripts. As mentioned before, I removed all GameObject.Find calls. Then, instead of using the board array to store either 1 or 0 if there's a block at that position or not, I have another array blocksLevel. There I store the Transform of the block itself. When the block is deleted, I set the accoring position in the array to Null.

I hope that might help some other beginner like me mitti2000

Comment
Add comment · 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
0

Answer by Yokimato · Apr 23, 2013 at 04:18 PM

You have a lot of GameObject.Find calls. These are slow when compared to what you could be doing.

I would suggest storing references to the block GameObjects in memory so that you do not have to call Find so much. I think not only with this solve your issue, but the game itself will probably run alot smoother (and it's a good practice to get into).

Comment
Add comment · Show 5 · 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 mitti2000 · Apr 23, 2013 at 04:40 PM 0
Share

Thanks for your input. I know I shouldn't have that many GameObject.Find calls. I just don't know how I should manage the script I posted without them. The thing is, that when the blocks get instantiated, they are children of an empty simly named "block(clone)". Every empty contains 4 of those blocks. As soon as they are set, I rename each of them accoring to their xy-position on the board. That way, I can "find" them again later when I have to delete them. Would it theoretically work if I would store the blocks in a 2D array? Or is there another way I could do that?

avatar image Yokimato · Apr 23, 2013 at 05:00 PM 0
Share

I think storing the gameobjects in a 2d array sounds like a great, simple way to keep your code easy to understand while keeping the performance where it needs to be.

avatar image mitti2000 · Apr 23, 2013 at 05:30 PM 0
Share

I'll try that. I'll let you know if that solved my problem tomorrow. Thanks again.

avatar image mitti2000 · Apr 24, 2013 at 06:02 AM 0
Share

Hi Yokimato I removed the GameObject.Find calls. The script definitly works better but still shows strange behaviour. Specially in the moveLines function. The blocks get destroyed but then not all blocks get moved... Is there a way to "pause" the update function until the subroutines are done? (if that would be a possible solution)

avatar image mitti2000 · Apr 25, 2013 at 07:47 AM 0
Share

Does anybody else have an idea how to solve my problem? Thanks

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

12 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

Related Questions

Script is causing immense lag, and I don't know what's causing it. 2 Answers

Access function (and variable) outside class in the class in Javascript. 1 Answer

Unity error 2 Answers

How to Update a score count going up and down 1 Answer

Migration from Unity 5.5.0f3 to Unity 2017- error and HoloLens app not building 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