- Home /
Cannot implicitly convert type `UnityEngine.Texture2D' to `UnityEngine.Material'
I have searched the internet for an answer to this, and found many similar queries, but nothing that combines my issues (and I'm quite new to this, so it's difficult for me to figure things when they're not explicitly explained...).
I am trying to download images from the internet and use them as textures on gameobjects that are instantiated at runtime.
The gameobjects are picture frames, and they have 3 places in their materials Array (size, element1: which is the frame design, element 2: which is the image inside - the one I want).
The following script is attached to the picture frame object:
public class ImageGetter : MonoBehaviour {
string url = "https://twitter.com/search?q=%22John%20Oliver%22&src=tren";
public List<string> list;
string imageID = "media";
System.Random random = new System.Random();
void Awake()
{
//Downloads URL
WebClient client = new WebClient();
var html = client.DownloadString(url);
//Searches URL for image addresses
string regexPattern = @"(https ?:) ?//?[^'<>]+?\.(jpg|jpeg|gif|png)";
MatchCollection matchList = Regex.Matches(html.ToString(), regexPattern, RegexOptions.IgnoreCase);
//Puts all images into a list
list = matchList.Cast<Match>().Select(match => match.Value).ToList();
list.RemoveAll(p => !p.Contains(imageID));
Debug.Log(list.Count);
}
IEnumerator Start()
{
Texture2D tex;
tex = new Texture2D(4, 4, TextureFormat.DXT1, false);
//Downloads random image from list
using (WWW www = new WWW(list[random.Next(0,list.Count)]))
{
//Makes image into texture
yield return www;
www.LoadImageIntoTexture(tex);
//applies texture to desired material
Renderer renderer = GetComponent<Renderer>();
renderer.materials[2] = tex;
}
}
}
The console returns: "Assets/Scripts/ImageGetter.cs(42,37): error CS0029: Cannot implicitly convert type UnityEngine.Texture2D' to
UnityEngine.Material'"
For the life of me I cannot figure out how to get this image into the desired materials spot in the array...
PLEASE HELP!..
Answer by ElijahShadbolt · Feb 26, 2018 at 06:13 AM
@Filhanteraren 's solution changes the first material's texture to your new texture, but you want the second material to be changed, right? Also, you cannot assign a texture to a Material that is null.
Create a new Material asset. It should default to the Unity Standard shader. We will make it so your texture is put into the Albedo slot at runtime.
In your MeshRenderer, assign this new material to be the second element in the Materials array.
Change your code like so:
Renderer renderer = GetComponent<Renderer>();
renderer.materials[1].mainTexture = tex;
This is untested, but I think it should work.
Edit: Dope! Forgot array indices start at 0.
Thank you!
Just for anyone else who reads this:
It turned out that I had to use renderer.materials[1]
This must be because the size field in the materials array doesn't count.
$$anonymous$$uch appreciated!
Isn't it just because array indices start at 0, so the second one is materials[1] ?
Answer by Filhanteraren · Feb 26, 2018 at 01:38 AM
This would be the correct way to assign a texture.
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = tex;
You are trying to assign a Texture2D as a material. The materials array in the renderer returns all the materials assigned to your renderer and are not the texture slot on the actual material
Thanks for answering. @Filhanteraren
When I do this, and the code compiles, but still doesn't assign the image. Is there something else I have to do to get a reference to the actual texture slot?
I just uploaded an image of the inspector where u can see the list of urls, and the texture slots in the renderer.materials In case it sheds light on anything.
Your answer
Follow this Question
Related Questions
What images do I have to bake in blender? 0 Answers
Tiling textures in material without changing the material. 0 Answers
Help with GUI, Textures and Materials (Health Circle) 1 Answer
How do I change the default material size in ProBuilder? 0 Answers
How to create a shader that makes the texture look far away? 0 Answers