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 /
  • Help Room /
avatar image
0
Question by Cmushi · Oct 05, 2016 at 02:36 PM · bitmap

Creating a map with tiles of different shapes by reading an image

Update:

I managed to narrow down the problem to the 'resource.load' function. It appears the function cannot load the bitmap or cannot find it since its returning a null. The bitmap 'provinces.bmp' is found inside folder 'Map' which is found inside folder 'Resources'. The function resource.load has the string "Assets/Resources/Map/provinces.bmp" as variable in order to find the path. I tried removing the .bmp from the path, remove the folder Map and even remove 'Assets/Resources' from the path. None have worked. Does anyone know why this is happening?

Original:

Hi,

I am following this tutorial about Procedural Cave Generation and decided to try and create a script that uses an image instead of using procedural generation. The image consists of a map that is made up of tiles (similar to this). The tiles have different shapes and each tile uses a unique RGBA code. I created a new project with an empty object, created the folder 'Map' to store the image and stored the folder in another folder named 'Resources'. I also created a script named 'MeshGenerator' and attached it to the empty object. The following is the code inside the script.

 using UnityEngine;
 using System.Collections;
 
 public class MeshGenerator : MonoBehaviour
 {
     public Texture2D mapImage;
     public string mapImagePath = "Assets/Resources/Map/provinces.bmp";
     private Color[,] mapArray;
 
     void Start()
     {
         ImageToArray();
     }
 
     public void ImageToArray()
     {
         mapImage = (Texture2D)Resources.Load(mapImagePath, typeof (Texture2D));
         mapArray = new Color[mapImage.width, mapImage.height];
         for (int x = 0; x < mapArray.GetLength(0); x++)
         {
             for (int y = 0; y < mapArray.GetLength(1); y++)
             {
                 mapArray[x, y] = mapImage.GetPixel(x,y);
             }
         }
     }
 
     public void OnDrawGizmos()
     {
         if(mapArray != null)
         {
             for (int x = 0; x < mapArray.GetLength(0); x++)
             {
                 for (int y = 0; y < mapArray.GetLength(1); y++)
                 {
                     Gizmos.color = mapArray[x, y];
                     Vector3 position = new Vector3(-mapArray.GetLength(0) / 2 + x + .5f, 0, -mapArray.GetLength(1) / 2 + y + .5f);
                     Gizmos.DrawCube(position, Vector3.one);
                 }
             }
         }
     }
 }

Method ImageToArray loads the image and copies the RGBA of every pixel in an array. OnDrawGizmos uses the array to draw the map. When I try to run the game, I get the following errors:

  • The referenced script on this Behaviour is missing!

  • The referenced script on this Behaviour (Game Object 'Map Generator') is missing!

  • NullReferenceException: Object reference not set to an instance of an object

  • MeshGenerator.ImageToArray () (at Assets/Scripts/MeshGenerator.cs:18)

  • MeshGenerator.Start () (at Assets/Scripts/MeshGenerator.cs:12)

What am I doing wrong exactly?

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
Best Answer

Answer by Cmushi · Oct 06, 2016 at 05:29 PM

Okay I managed to fix this.

  • First don't include 'Assests/Resources' in the path.

  • Second, remove the file extension of the image in the path (.bmp).

  • Third, make sure that the the file can be read by going in the inspector, change 'texture type' to 'advanced' and tick 'read/write enabled'.

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

Namespace Media does not exist 1 Answer

How to show both sides of a plane 2 Answers

Bitmap texture from 3DS Max not importing 2 Answers

How can I convert a PhotoCaptureFrame byte data into SoftwareBitmap? 0 Answers

FBX import: Bitmap not linking to object 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