Unity Useful Snippets

I made this post to compile some of useful snippets that I frequently use in my coding. So instead of searching around again and again, I better to list them here in one place.

How to set object tag by script in Unity

This one apparently so simple. To set a tag of a game object, for example I have game object: “myobject”, I can set its tag by typing: myobject.tag = newtag.

How to convert Unity raw image texture to Texture2D

Let’s say we have a raw image object in our Unity canvas, and we want to convert it’s image to Texture2D. Here is what we do on our scripting: First we declare a texture2d variable, for example: Texture2D myt2d; And then, we grab that raw image game object’s texture (let’s say we have myrawimage as the Raw Image) this way: myt2d = myrawimage.texture as Texture2D; And that’s all, we have our myt2d ready to use.


Leave a Reply

Your email address will not be published.