我正在尝试将组件(材质)添加到多个多维数据集中。 我有一系列的立方体,然后他们必须画画。在第一个多维数据集之后,Unity会抛出一个错误:
MissingComponentException:没有'材料'附在 "立方"游戏对象,但脚本正试图访问它
我的代码:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameScene : MonoBehaviour {
SceneManager Changer;
public Scrollbar speed, size;
GameObject[,] cubes;
int sizer;
Color mixer;
// Use this for initialization
void Start () {
size.value = PlayerPrefs.GetFloat("sizeSecond");
speed.value = PlayerPrefs.GetFloat("speed");
SetSize();
cubes = new GameObject[sizer, sizer];
GenerateCubes();
mixer = new Color(PlayerPrefs.GetFloat("red"), PlayerPrefs.GetFloat("green"), PlayerPrefs.GetFloat("blue"));
}
// Update is called once per frame
void Update () {
}
public void GoBack() {
SceneManager.LoadScene(2);
PlayerPrefs.SetInt("chosenScene", 1); //key setzen
PlayerPrefs.SetFloat("speed", speed.value);
PlayerPrefs.SetFloat("sizeSecond", size.value);
}
void SetSize() {
if (PlayerPrefs.GetInt("size") == 0)
{
sizer = 20;
}
else if (PlayerPrefs.GetInt("size") == 1)
{
sizer = 40;
}
else if (PlayerPrefs.GetInt("size") == 2)
{
sizer = 60;
}
}
void GenerateCubes() {
for (int i = 0; i < sizer; i++)
{
for (int j = 0; j < sizer; j++)
{
cubes[i, j] = GameObject.CreatePrimitive(PrimitiveType.Cube); //erzeugen
cubes[i, j].transform.position = new Vector3(i -sizer / 2, j - sizer / 2, 0); // durch 2 damit es mittig angezeigt wird.
cubes[i, j].GetComponent<Material>().color = mixer;
Instantiate(cubes[i, j]);
}
}
}
}
sizer获取多维数据集的数量。