• 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 AmasterAmaster · Jan 12, 2015 at 08:07 AM · imageloops

Color[][] is not storing color. Object reference not set to an instance of an object.

Hello, I am trying to reverse an image of a texture2D. I have tried many different attempts at this problem to no avail (including, but not limited to: Reverse() of arraylist, mixing jagged arrays, and looping through individual pixels). Here is a snippet of code for the reverse function.

 void ReverseImageX(Texture2D image)
     {
         //Get the array ready
         Color[][] newColorArrayWidth = new Color[image.height][];
 
         //Deconstruct the picture and get every pixel
         for(int i = 0; i < image.height; i++)
         {
             //Get the width of the picture
             for(int j = image.width - 1; j >= 0; j--)
             {
                 newColorArrayWidth[i][j] = image.GetPixel(j, i); //Error here: NullReferenceException: Object reference not set to an instance of an object
             }
         }
 
         //Reconstruct the picture reveresed in the X-axis
         for(int i = 0; i < image.height; i++)
         {
             //use the width of the picture to reconstruct the image
             for(int j = 0; j > image.width; j++)
             {
                 //Reconstruct
                 image.SetPixel(j, i, newColorArrayWidth[i][j]);
             }
         }
     }

I am also trying to not only reverse the image horizontally, but also vertically. But I assume that it is just messing with the for loops to get that desired effect.

The error is happening on line 364 (in this case it is: "newColorArrayWidth[i][j] = image.GetPixel(j, i);") and is not storing the color in the multidimensional array. I know this function is working by reciving the image through the parameter and going through the loop conditions, but stops at that line.

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 meat5000 ♦ · Jan 12, 2015 at 01:01 PM 0
Share

It seems you are creating an undefined size array and not really populating it either. Creating a colour based on the height of the image seems to not be what you are looking for. Your array does not contain the information from the Texture.

Using code warriors answer, simply swap the reversed height parameters for the horizontal ones.

2 Replies

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

Answer by code_warrior · Jan 12, 2015 at 11:08 AM

The enclosed function shows an easier way to flip a texture horizontally and vertically.

 Texture2D FlipTexture(Texture2D original){
            Texture2D flipped = new Texture2D(original.width,original.height);
            int xN = original.width;
            int yN = original.height;
            for(int i=0;i<xN;i++){
               for(int j=0;j<yN;j++){
                   flipped.SetPixel(xN-i-1, yN-j-1, original.GetPixel(i,j));
               }
             }
     flipped.Apply();
     return flipped;
 }
 

Note code is not tested code_warrior

Comment
Add comment · Show 1 · 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 AmasterAmaster · Jan 12, 2015 at 12:45 PM 0
Share

Alright, this works, but it flips it vertically (And I am happy it is at least flipping). Is there any advice you can give that would help me flip this horizontally? (I imaging that I may have to switch the for loops or switch the algorithm while setting the pixel). And thank you again for helping me with this problem.

avatar image
0

Answer by zharik86 · Jan 12, 2015 at 08:07 AM

For multidemention array use:

  void ReverseImageX(Texture2D image) {
   //Get the array ready
   //Create right multidemention array
   Color[ , ] newColorArrayWidth = new Color[image.height, image.width];
 
   //Deconstruct the picture and get every pixel
   
   for(int i = 0; i < image.height; i++) {
    //Get the width of the picture
    for(int j = image.width - 1; j >= 0; j--) {
     //In your case newColorArrayWidth[i][j] is not always GetPixel(j, i), only square picture
     newColorArrayWidth[image.width - 1 - j , i] = image.GetPixel(j, i); //get right colors and position into array
    }
   }

   //Reconstruct the picture reveresed in the X-axis
   for(int i = 0; i < image.height; i++) {
    //use the width of the picture to reconstruct the image
    for(int j = 0; j > image.width; j++) {
     //Reconstruct
     image.SetPixel(j, i, newColorArrayWidth[j, i]);
    }
   }
   image.Apply(); //apply to memory
  }

I hope that it will help you.

P.S.: If you want use array[][], than you must initialization it:

  Color[][] newColorArrayWidth = null;
  newColorArrayWidth = new Color[image.height][];
  for(int i = 0; i < image.height; i++) {
   newColorArrayWidth[i] = new Color[image.width];
  }
Comment
Add comment · Show 4 · 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 AmasterAmaster · Jan 12, 2015 at 09:18 AM 0
Share

Ok, I will give that a try. I will let you know if it worked out or not.

avatar image AmasterAmaster · Jan 12, 2015 at 12:41 PM 0
Share

Ok, I have tried this (as well as mixing and matching some arrays around) and it did get rid of the error I was having (thank you very much). But when I ran it, it did nothing to flip the texture. $$anonymous$$aybe it had something to do with the logic I was messing with, but thank you for enlightening me on the array problem.

avatar image zharik86 · Jan 13, 2015 at 07:18 AM 0
Share

@AmasterAmaster After change pixels you must apply texture to memory. I add it to my answer.

avatar image AmasterAmaster · Jan 14, 2015 at 10:04 AM 0
Share

Ok, and thanks again for the help, only if messing with textures were as simple as messing with game objects (when thinking in a higher level: scaling backwards), but I see that it is not that simple for this game engine when it comes to pixels. I do appreciate the help.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Overlapping Transparent Objects to Same Colour 0 Answers

new UI image, button 1 Answer

How do I make a fog wall 1 Answer

Unity Opencv 1 Answer

Using Image Component in OSX standalone app crashes build 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