0

I can't manage to initialize Texture2D hMap. I keep getting NullReferenceException: Object reference not set to an instance of an object. What am I doing wrong? I used it in line 11 with GetPixel but my code recognizes hMap as null. How do I initialize hMap properly?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Math = System.Math;

public class TerrainFace {
    Mesh mesh;
    int resolution;
    Vector3 localUp;
    Vector3 axisA;
    Vector3 axisB;
    public Texture2D hMap;
    float radius = 10;
    
    public TerrainFace(Mesh mesh, int resolution, Vector3 localUp)
    {
        this.mesh = mesh;
        this.resolution = resolution;
        this.localUp = localUp;

        axisA = new Vector3(localUp.y, localUp.z, localUp.x);
        axisB = Vector3.Cross(localUp, axisA);
    }
for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int i = x + y * resolution;
                Vector2 percent = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = PointOnCubeToPointOnSphere(pointOnUnitCube);
                Coordinate coordinate = CoordinateSystem.PointToCoordinate(pointOnUnitSphere);
                PlanarValue planarValue = CoordinateSystem.CoordinateToPlanarValue(coordinate);
                Color grayscale = hMap.GetPixel((int)planarValue.Xvalue,(int)planarValue.Yvalue);

                vertices[i] = pointOnUnitSphere*radius;

0 Answers0