Reputation:
So I think I'm missing a library here. Instead of 'MeshRenderer', I'm using 'MeshRenderer[]' in order to string an array of entries. In short, I'm trying to upload separate images to each child of a prefab. I get the following error message.
using UnityEngine;
using UnityEditor;
using System.IO;
public class WallClick : MonoBehaviour
{
string path;
public MeshRenderer col;
public MeshRenderer[] boxCols;
void OnMouseDown()
{
boxCols = GetComponentsInChildren<MeshRenderer>();
path = EditorUtility.OpenFilePanel("Overwrite with png", "", "png");
GetImage();
}
void EnableChildComponents()
{
foreach (MeshRenderer col in boxCols)
{
col.enabled = true;
}
}
void GetImage()
{
if (path != null)
{
UpdateImage();
}
}
void UpdateImage()
{
byte[] imgByte = File.ReadAllBytes(path);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(imgByte);
boxCols.material.mainTexture = texture; //Error here
}
}
Upvotes: 0
Views: 66
Reputation: 2427
boxCols is an array, you need to reference one of the elements in the array, which will be of type MeshRenderer. eg:
boxCols[0].material.mainTexture = texture;
Upvotes: 1