#null error in a referenced array.

1 messages · Page 1 of 1 (latest)

haughty ore
#

This is the code referencing the array

Debug.Log(enemyControllerScript?.gridCode.grid.Count);

The code it is referring to is this

public class Grid_Controller : MonoBehaviour
{
public float height;
public float length;

public int xOffset;
public int yOffset;
public float scale;

private Vector3 objectPos;

public List<GameObject> grid = new List<GameObject>();
private int gridNum;

public GameObject GridBox;

public SpriteRenderer sprite;
// Start is called before the first frame update
void Start()
{
    objectPos = gameObject.transform.position;
    
    length = objectPos.x + length;
    height = objectPos.y + height;

    Init();
}


public GameObject temp;
private bool hasBeenSpawned = false;
void Init()
{
    for (int x = (int)objectPos.x; x < length; x++)
    {
        for (int y = (int)objectPos.y; y < height; y++)
        {
            temp = Instantiate(GridBox, new Vector2(x / scale, y / scale), Quaternion.identity);
            grid.Add(temp);

            for (int i = 0; i < grid.Count; i++)
            {
                grid[i].transform.localScale = new Vector3(scale, scale, scale) / scale;
            }
            

            if (hasBeenSpawned == false)
            {
                
                Color color;
                ColorUtility.TryParseHtmlString("#0E4CE6", out color);
                sprite.color = color;
              
                hasBeenSpawned = true;
            }
            else if (hasBeenSpawned == true)
            {
                Color color2;
                ColorUtility.TryParseHtmlString("#0633A4", out color2);
                sprite.color = color2;
               
   
                hasBeenSpawned = false;
            }
            
        }
    }
}
}

}

#

grid.count works perfectly fine within this script but as soon as i reference it it returns null