texture texture2D

Hello all :slight_smile:

It seems am currently entering another weird issue
of unity. Maybe can you answer it this time ?
It’s about a script in C# i tried to write for texture
writing.
Here is the code:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
** private Color cc;**
** private int x,y;**

** // Use this for initialization**
** void Start () {**

** }**

** // Update is called once per frame**
** void Update ()**
** {**
** Texture2D ttt;**
** ttt= renderer.material.GetTexture(“_MainTex”);**

** if(!ttt)**
** {**
** Debug.Log(“dun work”);**
** return;**
** }**

** for (y=0; y < 10; ++y)**
** {**
** for (x=0; x < 10; ++x)**
** {**
** cc = ((x&y) != 0) ? Color.white : Color.red;**
** ttt.SetPixel (x, y, cc);**
** }**
** }**
** ttt.Apply();**
** }**
}

Ouchies ! sorry ! indentation has gone :sweat_smile:
This code won’t compile and says:
Cannot implicitly convert type UnityEngine.Texture' to UnityEngine.Texture2D’. An explicit conversion exists (are you missing a cast?)

OK OK, then i put a cast…
this becomes:
ttt= (Texture2D)renderer.material.GetTexture(“_MainTex”);

… and the compile runs OK… but at runtime i got an error:
InvalidCastException: Cannot cast from source type to destination type.

it appears that GetTexture returns a texture, as a Texture2D is awaited…
Anyone would have an idea on how to make an assignment of a Texture2D with a texture please ?

And please don’t tell me to do this in java :wink:

Thanks a lot for your help !

Try :
ttt= renderer.material.GetTexture(“_MainTex”) as Texture2D;

1 Like

WEEEEEE :stuck_out_tongue:
Works much better !!!
thanks a lot ! it appears i still have some
texture format problem, but this should be solved quickly…

thanks again !

For nothing, mate. Had the same problem for almost a hour this morning.

-Jedy

Thanks a lot =)