user6411634
user6411634

Reputation:

Upload separate images to each child of a prefab

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. enter image description here

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

Answers (1)

lockstock
lockstock

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

Related Questions