Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bee_Bone · Feb 26, 2018 at 01:05 AM · materialstextures

Cannot implicitly convert type `UnityEngine.Texture2D' to `UnityEngine.Material'

alt textI 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!..

screen-shot.png (45.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bee_Bone · Feb 26, 2018 at 06:49 AM 0
Share

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!

avatar image Bonfire-Boy Bee_Bone · Feb 26, 2018 at 03:27 PM 1
Share

Isn't it just because array indices start at 0, so the second one is materials[1] ?

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bee_Bone · Feb 26, 2018 at 01:54 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges