Я пытаюсь изменить игровые объекты, в частности кубы, чтобы изменить материал во время выполнения.
Вместо того, чтобы искать материал по имени, я решил попробовать другой подход, назначить несколько игробъекты с предварительно установленными материалами, затем, когда я хочу изменить материал чего-либо, я пытаюсь получить его от этих предварительно сделанных объектов.
Я уже потратил некоторое время, пытаясь это исправить, нашел несколько возможныхошибки, исправили их, но я все еще не получил нужный результат.
И я также использую отладку, предоставлю больше информации ниже.
Итак, я сначала создал несколько материалов какглобальные переменные.
public Material MaterialGrass;
public Material MaterialRock;
public Material MaterialSand;
public Material MaterialWater;
Затем я пытаюсь загрузить фактические материалы из предустановленных игровых объектов.
GameObject MainVariablesObject = GameObject.Find("MainVariablesObject");
GlobalVariablesScript thisGlobalVariablesScript = MainVariablesObject.GetComponent<GlobalVariablesScript>();
thisGlobalVariablesScript.MaterialGrass = GameObject.Find("GrassTexture").GetComponent<MeshRenderer>().materials[0];
thisGlobalVariablesScript.MaterialRock = GameObject.Find("RockTexture").GetComponent<MeshRenderer>().materials[0];
thisGlobalVariablesScript.MaterialSand = GameObject.Find("SandTexture").GetComponent<MeshRenderer>().materials[0];
thisGlobalVariablesScript.MaterialWater = GameObject.Find("WaterTexture").GetComponent<MeshRenderer>().materials[0];
Вид игры разделен на 9 квадратов.
GameObject CubeFront0 = GameObject.Find("CubeFront0");
GameObject CubeFront1 = GameObject.Find("CubeFront1");
GameObject CubeFront2 = GameObject.Find("CubeFront2");
GameObject CubeLeft0 = GameObject.Find("CubeLeft0");
GameObject CubeLeft1 = GameObject.Find("CubeLeft1");
GameObject CubeLeft2 = GameObject.Find("CubeLeft2");
GameObject CubeRight0 = GameObject.Find("CubeRight0");
GameObject CubeRight1 = GameObject.Find("CubeRight1");
GameObject CubeRight2 = GameObject.Find("CubeRight2");
А теперь часть, которую мне так и не удалось решить:
public static void DisplayPlayerView()
{
GameObject MainVariablesObject = GameObject.Find("MainVariablesObject");
GlobalVariablesScript thisGlobalVariablesScript = MainVariablesObject.GetComponent<GlobalVariablesScript>();
DefineTexturesSources();
GetSurroundingCells();
GameObject CubeFront0 = GameObject.Find("CubeFront0");
GameObject CubeFront1 = GameObject.Find("CubeFront1");
GameObject CubeFront2 = GameObject.Find("CubeFront2");
GameObject CubeLeft0 = GameObject.Find("CubeLeft0");
GameObject CubeLeft1 = GameObject.Find("CubeLeft1");
GameObject CubeLeft2 = GameObject.Find("CubeLeft2");
GameObject CubeRight0 = GameObject.Find("CubeRight0");
GameObject CubeRight1 = GameObject.Find("CubeRight1");
GameObject CubeRight2 = GameObject.Find("CubeRight2");
GameObject[] Cubes = new GameObject[] { CubeFront0, CubeFront1, CubeFront2,
CubeLeft0, CubeLeft1, CubeLeft2,
CubeRight0, CubeRight1, CubeRight2};
char[] Map = new char[] { thisGlobalVariablesScript.Front0, thisGlobalVariablesScript.Front1, thisGlobalVariablesScript.Front2,
thisGlobalVariablesScript.Left0, thisGlobalVariablesScript.Left1, thisGlobalVariablesScript.Left2,
thisGlobalVariablesScript.Right0, thisGlobalVariablesScript.Right1, thisGlobalVariablesScript.Right2};
for (int i = 0; i < 9; i++)
{
Material thisMaterial = Cubes[i].GetComponent<MeshRenderer>().materials[0];
//MeshRenderer thisMaterial = Cubes[i].GetComponent<MeshRenderer>();
if (Map[i] == 'G')
thisMaterial = thisGlobalVariablesScript.MaterialGrass;
if (Map[i] == 'R')
thisMaterial = thisGlobalVariablesScript.MaterialRock;
if (Map[i] == 'S')
thisMaterial = thisGlobalVariablesScript.MaterialSand;
if (Map[i] == 'W')
thisMaterial = thisGlobalVariablesScript.MaterialWater;
Cubes[i].GetComponent<MeshRenderer>().materials[0] = thisMaterial;
Debug.Log(Cubes[i].name + " " + Cubes[i].GetComponent<MeshRenderer>().materials[0]);
}
Что я нашел, разместив эти команды Debug.Log:
1) Все игровые объектыЯ пытаюсь "найти" существовать, нет ошибки онre.
2) «thisMaterial» получает правильный материал, поэтому я думаю, что нет проблем с определением материалов в качестве переменных и получением правильных данных.
3) Я больше не получаю ошибку, котораяматериал не присоединен.
4) Внутри цикла «for» thisMaterial отображает новые и правильные данные, а «Cubes [i] .GetComponent (). materials [0]» отображает старые данные.- материалы, которые я установил вручную в начале.
5) Данные в массиве "Карта" верны.
Что мне не хватает?