Change Skybox Material Programmatically (c#)

1.9k views Asked by At

I want to change the default skybox material so I made this lines... After that I only see blue... What's my mistake?

the material is on the Assets folder by the away

Material levelMat = Resources.Load(Application.dataPath + "/testeVR.mat", typeof(Material)) as Material;
RenderSettings.skybox = levelMat;
2

There are 2 answers

0
Programmer On BEST ANSWER

the material is on the Assets folder by the away

You can't place the material in the Asset folder if you are loading it with Resources.Load.

After that I only see blue

The levelMat variable is null. You get blue when the material you are applying to the Skybox is null and you can prove this by adding Debug.Log(levelMat); after it.

Material levelMat = Resources.Load(Application.dataPath + "/testeVR.mat", typeof(Material)) as Material;

You can't do this either. The Application.dataPath should not be used in the path parameter of the Resources.Load function.

Few things to understand from your code:

1.Resources.Load requires a special folder called Resources. Create a folder called Resources in the Assets directory then place the testeVR.mat inside it.

2.You do not put the file extension in the path parameter of the Resources.Load function. So, testeVR.mat should actually be testeVR without ".mat".

Simply create a folder called Resources in the Assets Folder then place the testeVR material inside this place. It should look like this: Assets/Resources/testeVR.mat. The code below should be used to load it.

Material levelMat = Resources.Load("testeVR", typeof(Material)) as Material;

Now, lets say that you have another folder called "Mats" which is in the Resources folder. You would use something like below:

Material levelMat = Resources.Load("Mats/testeVR", typeof(Material)) as Material;
1
Maaz Irfan On

just create public variable of material and place the material you want to be load during runtime or if you want to load it via some function use this

RenderSetting.sky = skyboxMat; 

skyboxMat stored the material which drag it in unity and then it becomes apart of your skybox

public Material skyboxMat;
voidstart(){ RenderSetting.skybox = skyboxMat;}