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 Durgal · Feb 28, 2017 at 07:37 AM · minimapinitializationpixels

Initializing a 2d Array of Classes

I'm trying to create a map system. What I'd like to do is have an object with a 2d array which contains a class (or struct) at each index. (pixelMap contains a 2d array of pixel chunks) When my map object (drawPixelsToMap) goes to draw, it selects each index and looks at three different values inside the struct to determine what color to draw and at what position on the map. Here are the two c# scripts I've created so far:

 // pixelMap.cs
 public class pixelChunk
 {
     private int aLevel;
     private int bLevel;
     private int cLevel;
 
     public pixelChunk()
     {
         this.aLevel = 0;
         this.bLevel = 0;
         this.cLevel = 0;
     }
 
     ~pixelChunk()
     {
         //test
     }
 
     public int getA() { return aLevel; }
     public int getB() { return bLevel; }
     public int getC() { return cLevel; }
 };
 
 public class pixelMap {
 
     const int SIZE = 500;
     pixelChunk[,] chunkMap = new pixelChunk[SIZE, SIZE];
 
     // Awake function
     void Awake() {
         for (int i = 0; i <= SIZE; i++)
         {
             for (int j = 0; j <= SIZE; j++)
             { chunkMap[i, j] = new pixelChunk(); }
         }
     }
 
     ~pixelMap()
     {
         //test
     }
 
     public int aLevel (int x, int y) { return chunkMap[x,y].getA(); }
     public int bLevel(int x, int y) { return chunkMap[x, y].getB(); }
     public int cLevel(int x, int y) { return chunkMap[x, y].getC(); }
 }
 

And then my draw map object:

 // drawPixelsToMap.cs
 public class drawPixelsToMap : MonoBehaviour {
 
     public pixelMap theMap;
     public RenderTexture renderTexture;
     public Renderer renderer;
     Texture2D texture;
 
     // init texture
     void Start () {
         texture = new Texture2D (renderTexture.width, renderTexture.height);
         renderer.material.mainTexture =  texture;
 
         theMap = new pixelMap();
     }
 
     // draws pixels based on where pixels exist in map
     // texture dimensions: 0 - 255 pixels
     void Update () {
         RenderTexture.active = renderTexture;
 
         texture.ReadPixels (new Rect (0, 0, renderTexture.width, renderTexture.height), 0, 0);
 
         for (int j = 0; j < renderTexture.height; j++)
         {
             for (int i = 0; i < renderTexture.width; i++)
             {
                 if (theMap.aLevel(i, j) != 0)
                 {
                     texture.SetPixel(i / 2, j / 2, new Color(1, 0, 0, .5f));
                 }
             }
         }
         
         texture.Apply (); 
 
         RenderTexture.active = null;
     }
 }
 

If I remove the if statement in my drawPixelsToMap class I can correctly draw pixels in a hard coded way with a desired color. But when I go through the debugger I notice that my pixelChunk objects are getting destroyed before I expect and that each pixelChunk is null. I suspect I'm not initializing the pixelChunks correctly. But I'm not certain. Any help on this would be greatly appreciated!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Durgal · Mar 01, 2017 at 07:22 AM

Exciting new developments here. Got er to work. The issue I had was that I going out of bounds in pixelMap and that instead of using a constructor, I just set each value to 0 on the first three lines of Chunk.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to get borders of UI-Mask? 0 Answers

Initialising List array for use in a custom Editor 1 Answer

Finding a gameobject in a level that has not been loaded 1 Answer

vector2D Array initialization not working. 3 Answers

Init code goes to Null unknow reason 2 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