Texture2D as private var

Maybe I’m just lost for nothing… but… how can I assign the value to a private variable of type Texture2D ?

I just want the same script to use the same texture in all the scenes it’s applied to, without using exposed variables.

A line of code like

private var goMap : Texture2D = “MyWall”;

returns "cannot convert ‘string’ to UnityEngine.Texture2D

what is the right syntax to assign to goMap the texture ?

You must load the texture from somewhere or pass it to the script. There are various ways.

“MyWall” is no texture, thats a name only. You must assign a Texture2D object

A simple approach would be either making that texture public or have a public texture that you use as “configuration slot” from where you get the Texture2D reference and assign it to the private variable

Many thanks, but how can you create a public texture ? How can I use it as a configuration slot ? Can you please provide one or two lines sample code ?

var tex : Texture2D;

at the start and in Update or whereever you want to check for goMap updates

goMap = tex;

Ok, that’s the first step… but if I want to assign goMap to texture “myWall” that is in my assets ?

Not 100% sure, but thats how we are doing it:
You must put them into the subfolder Resources in your Assets folder and use Instantiate(Resources.Load( … ), … )
But don’t forget that you must destroy those resources yourself again as well!

Yep ! That worked out, many thanks !

Can you elaborate a little more about the necessity to destroy the resources yourself?