Пытаюсь заставить Gizmo менять цвет при прикосновении к игровым объектам.C#

Место общения программистов C#
Anonymous
Пытаюсь заставить Gizmo менять цвет при прикосновении к игровым объектам.

Сообщение Anonymous »

Я пытаюсь заставить мои вещицы менять цвет, когда они касаются игрового объекта слоем препятствия. однако по какой-то причине штуковины не появляются и выдают ошибку «ссылка на объект не установлена ​​на экземпляр объекта»

Код: Выделить всё

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

public class Grid : MonoBehaviour

{
Node[,] grid;
[SerializeField] int width = 25;
[SerializeField] int length = 25;
[SerializeField] float cellSize = 1f;
[SerializeField] LayerMask obstacleLayer;

private void Start()
{
GenerateGrid();
}

private void GenerateGrid()
{
grid = new Node[length, width];
CheckPassableTerrain();
}

private void CheckPassableTerrain()
{
for (int y = 0; y < width; y++)
{
for (int x = 0; x < length; x++)
{
Vector3 worldPosition = GetWorldPosition(x, y);
bool passable = !Physics.CheckBox(worldPosition, Vector3.one * cellSize, Quaternion.identity, obstacleLayer);
grid[x, y] = new Node();
grid[x, y].passable = passable;
}
}
}

private void OnDrawGizmos()
{
if (grid == null)
{
for (int y = 0; y < width; y++)
{
for (int x = 0; x < length; x++)
{

Vector3 pos = GetWorldPosition(x, y);
Gizmos.color = grid[x, y].passable ? Color.white : Color.red;
Gizmos.DrawCube(pos, Vector3.one );
}
}
}
}

private Vector3 GetWorldPosition(int x, int y)
{
return new Vector3(transform.position.x + (x * cellSize), 0.5f, transform.position.z + (y * cellSize));
}

}

Я попробовал изменить строку на gizmos.color = color.red, которая порождает штуковины, и это заставляет меня думать, что это как-то связано с тем, что сетка по умолчанию имеет нулевое значение. что я тоже не знаю, как исправить.

Подробнее здесь: https://stackoverflow.com/questions/789 ... me-objects

Вернуться в «C#»