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 scottstephan · Nov 08, 2013 at 09:18 AM · crashfreezegetpixels

Hardcrash when using GetPixels

I have some pretty simple code- I get the pixels of a texture and then check to see how many of those pixels are clear. Here's the issue: It hard freezes Unity whenever I run it.

Two Q's: 1- Any idea why? The portion that freezes is the "checkTexture()" function 2- Is there a smarter way to be checking how many pixels are x color?

UPDATE- Its definitely the loop that's crashing this. But why?

 using UnityEngine;
 using System.Collections;
 
 public class scratchoffManager : MonoBehaviour {
     public GameObject front;
     public GameObject back;
     private Texture2D texture;
     
     private Color[] pixelHitBlock = new Color[400];
     private gameManager manager;
     
     private int curTexSize;
     private Color[] currentTex;
     private int clearCounter;
     void Start () {
         manager = GameObject.Find ("gameManager").GetComponent<gameManager>();
         
         for(int i = 0; i < pixelHitBlock.Length; i++){
             pixelHitBlock[i] = new Color(0f,0f,0f,0f);
         }
     }
     
     public void setTexture(Texture2D newTex){
         back.renderer.material.mainTexture = newTex; //setTexture
         Debug.Log("Set back tex?");
         texture = Instantiate(front.renderer.material.mainTexture) as Texture2D;  //clone the material
         front.renderer.material.mainTexture = texture; //set the material equal to the clone   
         
         curTexSize = texture.height * texture.width;
         currentTex = new Color[curTexSize];
         
         StartCoroutine("checkTimer");
     }
     
     public void adjustTexture(Vector2 texCo){ //always come in a s0,0
         int x = Mathf.FloorToInt(texCo.x * texture.width);
         int y = Mathf.FloorToInt(texCo.y * texture.height);
         Debug.Log("pixel hit at" + x + "," + y);
                 
         texture.SetPixels(x,y,20,20,pixelHitBlock);
         texture.Apply();
     }
     
     private void checkTexture(){
         Debug.Log("Checking the texture");
         currentTex = texture.GetPixels(0,0,texture.width,texture.height);
         Debug.Log("Checking the texture 2");
         for(int i = 0; i < currentTex.Length; i++){
             if(currentTex[i] == Color.clear){
                 clearCounter++;
                 Debug.Log("Checking the texture" + i);
             }
             Debug.Log("Clear counter:" + clearCounter);
             clearCounter = 0;
         }
     }
     
     IEnumerator checkTimer(){
         yield return new WaitForSeconds(1.5f);
         
         checkTexture();
         StartCoroutine("checkTimer");
     }
 }
 
Comment
Add comment · Show 1
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 flamy · Nov 08, 2013 at 10:31 AM 0
Share

is "Checking the texture 2" gets printed.

1 Reply

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

Answer by ArkaneX · Nov 08, 2013 at 10:32 AM

The freeze is caused by Debug.Log statements. Even if your texture is relatively small, let's say 128x128, printing once for each pixel can easily take over 20 seconds.

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 scottstephan · Nov 08, 2013 at 05:00 PM 0
Share

Give this man a medal- If I was running the function once, we'd be fine. But because it was on a Corountine loop, this was creating an infinity backup of work. Removed the debug and everything is peachy!

avatar image doomtoo · Nov 28, 2017 at 02:55 AM 0
Share

Thanks! I was stuck on this too, was going to try to find the inner workings of getpixels! This will save me some major time debugging.

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

18 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

Related Questions

In 5.2.1f1 Editor freezes in play mode 3 Answers

Unity crash when I add a Vector2 to a List 0 Answers

Troubleshooting desktop build receiving unhandled NULL exception 0 Answers

Unity crashes often when the new scenes are being loaded 1 Answer

Unity 3D Keep Crashing in every site! 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