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 rettichmann · Dec 20, 2016 at 03:25 PM · imagecolor changemultiple objectsreadpixels

Assign image color values to multiple primitives

Hi! I have a problem with assigning color information from an image to a grid of primitives. I want to rebuild the image with primitives to use the third dimension for storing information. It will be for data vis purposes.

// Error-Log: NullReferenceException: Object reference not set to an instance of an object DrawPrimitives.Start () (at Assets/nuilab/Scripts/DrawPrimitives.cs:13)

I am relatively new to Unity, C# and coding in general. I handled NullReferenceExeptions by myself until now. I do not find the problem. I hope that someone can lend me his or her eyes to track the issue.

Here is the first script which grabs the color information:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GetTextureColors: MonoBehaviour
 {
     public Texture2D Tex;
     public int skip = 10;
 
     Color[] AllColors;
 
     //-------------------------------------------------------------------
     void Start()
     {
         List<Color> ListOfColors = new List<Color>();
 
         for (int x = 0; x < Tex.width; x += skip)
         {
             for (int z = 0; z < Tex.height; z++)
             {
                 int currentPixel = x + z * Tex.width;
                 Color currentColor = Tex.GetPixel(x, z);
                 ListOfColors.Add(currentColor);
             }
         }
         AllColors = ListOfColors.ToArray();
     }
 
     //-------------------------------------------------------------------
     public Color[] GetColors()
     {
         return AllColors;
     }
 }

Here is the second script that builds all primitives:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class DrawPrimitives: MonoBehaviour {
 
     GetTextureColors GetTextureColorsScript;
     Texture2D Texture;
     Color[] currentColor;
     //-------------------------------------------------------------------
     void Start()
     {
         currentColor = GetTextureColorsScript.GetColors();
         GetTextureColorsScript = GetComponent<ChangeTexture>();
         Texture = GetTextureColorsScript.Tex;
 
         GeneratePrimitives();
     }
 
     //-------------------------------------------------------------------
     void GeneratePrimitives()
     {
         int skip = ChangeTextureScript.skip;
         for (int x = 0; x < Texture.width; x += skip)
         {
             for (int z = 0; z < Texture.height; z += skip)
             {
                 int currentPixel = x + z * Texture.width;

                 Vector3 vertex = new Vector3(x * 0.1f, 0, z * 0.1f);

                 GameObject Point = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 Point.transform.position = vertex;
                 Point.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);

                 Renderer rend = GetComponent<Renderer>();
                 rend.sharedMaterial.shader = Shader.Find("Cube_Material");
                 rend.sharedMaterial.SetColor("_Color" + currentPixel.ToString(), currentColor[currentPixel]);  
             }
         }
     }
 }




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 rettichmann · Dec 21, 2016 at 03:03 PM

I found the problem. I was accessing the render component of the current gameObject instead of the instance gameObjects.

Old code:

 Renderer rend = GetComponent<Renderer>();
 rend.sharedMaterial.SetColor("_Color" + currentPixel.ToString(), currentColor[currentPixel]);  

New code:

 Renderer rend = Point.GetComponent<Renderer>();
 rend.material.color = currentColor[currentPixel];
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
1

Answer by lamphung719 · Dec 21, 2016 at 09:16 AM

Did you attach GetTextureColors script to the same object that uses your method? Because if you didn't, your variant GetTextureColorsScript is null.

Comment
Add comment · Show 6 · 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 rettichmann · Dec 21, 2016 at 09:35 AM 0
Share

Thank you for your reply!

Here is my GameObject with attached scripts: Link to Inspector Screenshot

avatar image lamphung719 rettichmann · Dec 21, 2016 at 09:48 AM 1
Share

change the order of code

      currentColor = GetTextureColorsScript.GetColors();
      GetTextureColorsScript = GetComponent<ChangeTexture>();

to

  GetTextureColorsScript = GetComponent<ChangeTexture>();
  currentColor = GetTextureColorsScript.GetColors();


because you have not assign GetTextureColorsScript to your variant, so you got null exception

avatar image rettichmann lamphung719 · Dec 21, 2016 at 10:05 AM 0
Share

Sorry for the different na$$anonymous$$g: GetTextureColorsScript = GetComponent(); It is actually: ChangeTextureScript = GetComponent();

That is a good hint! Thank you! Unfortunately a new Exeption appears.

NullReferenceException: Object reference not set to an instance of an object DrawPrimitives.GeneratePrimitives () (at Assets/nuilab/Scripts/DrawPrimitives.cs:35) DrawPrimitives.Start () (at Assets/nuilab/Scripts/DrawPrimitives.cs:18)

Show more comments
avatar image rettichmann · Dec 21, 2016 at 02:15 PM 0
Share

With the help of @lamphung719 I managed to get rid of the error message. Unfortunately a new one popped up.

Error-Log: NullReferenceException: Object reference not set to an instance of an object DrawPrimitives.GeneratePrimitives () (at Assets/nuilab/Scripts/DrawPrimitives.cs:35) DrawPrimitives.Start () (at Assets/nuilab/Scripts/DrawPrimitives.cs:18)

DrawPrimitives.cs:18

 GeneratePrimitives();

DrawPrimitives.cs:35

 rend.shared$$anonymous$$aterial.shader = Shader.Find("Cube_$$anonymous$$aterial");

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to avoid color multiplication with UI.Graphic.CrossFadeColor? 1 Answer

How to change the color of an image every two seconds 1 Answer

Using Texture2D.GetPixels() to take a screenshot and then show it on an Image - iOS problems. 1 Answer

Image UI color not changing when sprite is set via script 0 Answers

How can I change color of an Image in script (png source) ? 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