0

I am learning to create a 2 dimensional array of 100x100 and input random number 1 and 0 into it then use gizmos to display the result. The code roughly demonstrate what I trying to do but it is not working and I failed to spot the mistake. Would appreciate ideas or hints on how to edit the code and point out the error.

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

public class NewBehaviourScript : MonoBehaviour
{
int x;
int y; 
public int[,] array1; 
// Start is called before the first frame update
void Start()
{  
    for (x = 0; x < 100; x++)
    {
        for (y = 0; y < 100; y++)
        {
            array1[x, y] = Random.Range(0, 2);
           
            Debug.Log(string.Format("{0},{1},{2}",x,y,array1[x,y]));
            
        }
    }
}
void OnDrawGizmos()
{
    if (array1[x, y] == 1)

    {
        Gizmos.color = Color.black;
    }
    else
    {
        Gizmos.color = Color.white;
    }
}
    
}
Serve Laurijssen
  • 8,981
  • 5
  • 35
  • 82
chuackt
  • 153
  • 5
  • 2
    You never instantiate your array.... `array1 = new int[100, 100];` – Hellium Dec 22 '20 at 09:47
  • Thanks, it fixed the error was using int x=100,int y=100 and int[,]array1=new int[x,y] and it wont work so I deleted it. Doesn't fully understand the meaning of initialization thought the for loops is the command that initialize it. – chuackt Dec 22 '20 at 10:05
  • 1
    This is neater: `Gizmos.color = array1[x, y] == 1 ? Color.black : Color.white;` – Enigmativity Dec 22 '20 at 10:05

0 Answers0