C# Array returns value 0

Hi i am pretty new to unity3D and porting my aktuell project from Java to Unity.

Now i have a problem and cant get the error.
I have a list that is made of a costum class witch holds some small data

when i try to acces the array stored in that class i get alyways the value 0 returned.

here is some code for bette understanding and sorry for the bad english.

The class that store the data

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RoomTemplate
{

    public int [,]             room_map;
    public List<Vector2i>  door_list;
    public int width;
    public int height;
   
    public RoomTemplate(int size_x, int size_y)
    {
        room_map     = new int [size_x,size_y];
        door_list     = new List<Vector2i>();
        this.width     = size_x;
        this.height = size_y;
    }
}

Where the Roomtemplate is filled

public RoomTemplate loadRoom(Texture2D pixmap)
{
     RoomTemplate room_template = new RoomTemplate(pixmap.width, pixmap.height);
     
     for(int x = 0; x < pixmap.width; x ++)
     {
       for(int y = 0; y < pixmap.height; y ++)
       {

         if(pixmap.GetPixel(x, y).r == floor_color.x
         && pixmap.GetPixel(x, y).g == floor_color.y
         && pixmap.GetPixel(x, y).b == floor_color.z)
         room_template.room_map[x,y] = 1; // floor
         // allready checked here and it stores the right value at x,y
       }
     }
     return room_template;
}

where it goes to

     files = Resources.LoadAll("Dungeon Parts/Short");

     foreach(Texture2D file in files)
     {
       pixmap = file as Texture2D;
       short_floor_template_list.Add(room_template_loader.loadRoom(pixmap) as RoomTemplate);
     }
     generateMap(200, 200, 10, 30, 50, 75, 95,0);

where it is needed and return 0 at short_floor_template_list[0].room_map[x,y]

public int[,] generateMap(int map_width, int map_height ,int short_floor ,int  long_floor ,int  small_room,int large_room, int huge_room, int max_rooms)
   {
     int[,] map = new int[map_width,map_height];
     if(max_rooms == 0)max_rooms = 499999999;

     // for testing
     for(int x = 0; x <  short_floor_template_list[0].width; x ++)
     {
       for(int y = 0; y <  short_floor_template_list[0].height; y ++)
       {           
         Debug.Log("tile value = " + short_floor_template_list[0].room_map[x,y]);
         map[x + map_width / 2,y + map_height / 2] = short_floor_template_list[0].room_map[x,y];
         if(map[x,y] == 2)
         {
           GameObject wall = Instantiate(Resources.Load("Wall")) as GameObject;
           wall.transform.position = new Vector3(x,1,y);
         }
       }
     }
[code]

Where’s your show_floor_template_list?

here is the full code and forgot to say i am also new to c# :slight_smile:

full code

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DungeonGenerator : MonoBehaviour {
    //no nested lists in c#
    //private List<List<RoomTemplate>> room_template_lists;

    private List<RoomTemplate> short_floor_template_list;
    private List<RoomTemplate> long_floor_template_list;
    private List<RoomTemplate> small_room_template_list;
    private List<RoomTemplate> large_room_template_list;
    private List<RoomTemplate> huge_room_template_list;  
    private List<Vector2i>       open_door_list;
    private List<Vector2i>       blocked_door_list;
  
    void Start ()
    {
        Texture2D  pixmap;
        Object[] files;

        //load the sample file
        files = Resources.LoadAll("Dungeon Parts");
        RoomTemplateLoader room_template_loader = new RoomTemplateLoader(files[0]);
        files = null;

//        room_template_lists             = new List<List<RoomTemplate>>();
        short_floor_template_list         = new List<RoomTemplate>();
        long_floor_template_list          = new List<RoomTemplate>();
        small_room_template_list        = new List<RoomTemplate>();
        large_room_template_list        = new List<RoomTemplate>();
        huge_room_template_list            = new List<RoomTemplate>();  
        open_door_list                    = new List<Vector2i>();
        blocked_door_list                = new List<Vector2i>();
      
  

        // Load room templates--------------------------------------------------------------


        files = Resources.LoadAll("Dungeon Parts/Short");

        foreach(Texture2D file in files)
        {
            pixmap = file as Texture2D;
            short_floor_template_list.Add(room_template_loader.loadRoom(pixmap));
        }

        files = Resources.LoadAll("Dungeon Parts/long");

        foreach(Texture2D file in files)
        {
            pixmap = file as Texture2D;
            long_floor_template_list.Add(room_template_loader.loadRoom(pixmap));
        }  
      
        files = Resources.LoadAll("Dungeon Parts/large");

        foreach(Texture2D file in files)
        {
            pixmap = file as Texture2D;
            large_room_template_list.Add(room_template_loader.loadRoom(pixmap));
        }  
      
        files = Resources.LoadAll("Dungeon Parts/small");

        foreach(Texture2D file in files)
        {
            pixmap = file as Texture2D;
            small_room_template_list.Add(room_template_loader.loadRoom(pixmap));
        }  
      
        files = Resources.LoadAll("Dungeon Parts/huge");

        foreach(Texture2D file in files)
        {
            pixmap = file as Texture2D;
            huge_room_template_list.Add(room_template_loader.loadRoom(pixmap));
        }  
        //-----------------------------------------------------------------------------------------
      
//        room_template_lists.Add(short_floor_template_list);
//        room_template_lists.Add(long_floor_template_list);
//        room_template_lists.Add(small_room_template_list);
//        room_template_lists.Add(large_room_template_list);
//        room_template_lists.Add(huge_room_template_list);

        pixmap = null;
        files  = null;

        Debug.Log(short_floor_template_list.Count + long_floor_template_list.Count + small_room_template_list.Count + large_room_template_list.Count + huge_room_template_list.Count + " rooms loaded");
  

        generateMap(200, 200, 10, 30, 50, 75, 95,0);

    }

    public int[,] generateMap(int map_width, int map_height ,int short_floor ,int  long_floor ,int  small_room,int large_room, int huge_room, int max_rooms)
    {
        int[,] map = new int[map_width,map_height];
        if(max_rooms == 0)max_rooms = 499999999;
        int rooms = 0;

        // for testing
        for(int x = 0; x <  short_floor_template_list[3].width; x ++)
        {
            for(int y = 0; y <  short_floor_template_list[3].height; y ++)
            {                  
                Debug.Log("tile value = " + short_floor_template_list[3].room_map[x,y]);
                map[x + map_width / 2,y + map_height / 2] = short_floor_template_list[3].room_map[x,y];
                if(map[x,y] == 2)
                {
                    GameObject wall = Instantiate(Resources.Load("Wall")) as GameObject;
                    wall.transform.position = new Vector3(x,1,y);
                }
            }
        }
//        for(Vector2i door : short_floor_template_list.get(0).door_list)
//        {
//            open_door_list.add(new Vector2i(door.x + map_width / 2,door.y + map_height / 2,door.direction));
//        }
//      
//        ArrayList<Vector2i> tmp_door_list= new ArrayList<Vector2i>();
//      
//        while(!open_door_list.isEmpty() && rooms <= max_rooms)
//        {
//            tmp_door_list.addAll(open_door_list);
//            open_door_list.clear();
//          
//            for(Vector2i door : tmp_door_list)  
//            {
//                int rnd = (int) (Math.random()* 100f);
//              
//                if(rnd >= huge_room)
//                {
//                    if(addRoom(door,4)== true)rooms ++;
//                }
//                else
//                    if(rnd >= large_room)
//                {
//                    if(addRoom(door,3)== true)rooms ++;
//                }
//                else
//                    if(rnd >= small_room)
//                {
//                    if(addRoom(door,2)== true)rooms ++;
//                }
//                else
//                    if(rnd >= long_floor)
//                {
//                    addRoom(door,1);
//                }
//                else
//                    if(rnd >= short_floor)
//                {
//                    addRoom(door,0);
//                }
//            }
//            tmp_door_list.clear();
//        }
//        checkBlockedDoors();
        return map;
    }
}

Are you absolutely sure that this “if” statement is true?

         if(pixmap.GetPixel(x, y).r == floor_color.x
         && pixmap.GetPixel(x, y).g == floor_color.y
         && pixmap.GetPixel(x, y).b == floor_color.z)
         room_template.room_map[x,y] = 1; // floor

I’m not certain what types you’re using here, but RGB color values are usually floating-point numbers, and testing whether two floating-point numbers are exactly equal is almost always a bad idea (due to precision issues).

You should probably figure out some way other than color to tell which spaces are floor. If you must compare colors, you should probably test whether they are approximately equal (e.g. using Mathf.Approximately).

I’d also strongly encourage you to use { } braces and proper indentation for your “if” statements, even when the inner block is only one line long. I didn’t realize what this code was doing the first time I read it because it looks (at a glance) like 4 separate lines of code rather than one “if” statement and a conditional clause. Very easy to make mistakes when your code is formatted strangely.

first of all thanks for the fast awnsers

you are right he realy returns false i checked it yesterday before i got to bed and i am pretty sure that it worked…
also i tought float errors occur only when working with floats not when cloning them.

I will give Mathf.Approximately a try pretty sure it will work thanks. :slight_smile:

and here a little screen what it is all about.
screen

Floats shouldn’t change if you directly copy them, but they could change if you cast them to other types and back (which sometimes can happen without you realizing it, if you’re not careful).

You usually shouldn’t use == on floats. But it’s possible you’re in the rare case where it’s OK; I don’t know enough about how your program works to say for sure.

Ok got it the problem is that i placed my sample image holding the 3 colors in a folder that contains other folders with Images i tought that Resources.LoadAll only load from the main folder :slight_smile:

    files = Resources.LoadAll("Dungeon Parts");
        RoomTemplateLoader room_template_loader = new RoomTemplateLoader(files[0]);

how it is right

Object sample= Resources.Load ("Dungeon Parts/sample_part");
        RoomTemplateLoader room_template_loader = new RoomTemplateLoader(sample as Texture2D);

Also i dont have to use Mathf.Approximately here is the Constructor of the template loader

    Vector3 door_color;
    Vector3 wall_color;
    Vector3 floor_color;

    // here he was loading the wrong image
    public RoomTemplateLoader(Texture2D pixmap)
    {
        wall_color = new Vector3(pixmap.GetPixel(0,0).r,pixmap.GetPixel(0,0).g,pixmap.GetPixel(0,0).b);
        floor_color = new Vector3(pixmap.GetPixel(1,0).r,pixmap.GetPixel(1,0).g,pixmap.GetPixel(1,0).b);
        door_color = new Vector3(pixmap.GetPixel(2,0).r,pixmap.GetPixel(2,0).g,pixmap.GetPixel(2,0).b);

    }