Good day.
I’m working on a project, where I have to add a Sprite to “Legacy Shaders/Transparent/Diffuse” material via script.
Now, I’m pretty new to Unity, but I manage to dig myself out of most problems by googling everything. But here I have finally met my demise :D.
In my code, I have my variables for a texture and a sprite, but due to the fact that I’m working with an extension, that lets you add Decals (Simple Decal System | Particles/Effects | Unity Asset Store) to your meshes dynamically, I am forced to add a sprite to the texture, I can’t use a Texture2D object. Now, it is possible to add the sprite to the material via the Editor, but for some reason, the material cannot take the sprite by script.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ImagesAndSpritesLoader : MonoBehaviour {
private Texture2D myImage;
private Sprite spriteImage;
private GameObject obj;
void Start () {
myImage = Resources.Load ("Decals/absa") as Texture2D;
spriteImage = Sprite.Create (myImage, new Rect (0.0f, 0.0f, myImage.width, myImage.height), new Vector2 (0.5f, 0.5f), 100.0f);
GetComponent<MeshRenderer> ().material.mainTexture = spriteImage;
}
}
Here is the console error:
Cannot implicitly convert type UnityEngine.Sprite' to
UnityEngine.Texture’
I would be glad for any answers, since I’ve been racking my brain for many hours with little success.