In my Assets folder I have a folder named Material where I stored all the needed materials, One of the materials in the Material folder is Night_Sky
, which I want at a certain moment of the game to replace the day_sky
and set Night_sky
as my default Skybox. I tried many codes all of them return null object, examples :
night = Resources.Load("Material", typeof(Material)) as Material;
or
night = Resources.Load("Material/Night_Sky.mat", typeof(Material)) as Material;
How can I load my Night_Sky
material, or if there is an easier way to switch my skybox to night_sky
thank you for sharing it
This will not work as Resources.Load requires you to place the object in the Resources folder. This information can also be found in the Unity Docs
For this to properly work you will need to create a folder called Resources Inside the Assets folder. After which you can add the Material folder to this. So the folder structure would look as following
Assets/Resources/Materials/Night_Sky.mat
Further more the script to load the material looks just fine.
If you really do not wish to use the resources folder, you can try to obtain the materials using System.IO folder search options instead. But I would advice you to just use the build in Resources function.
When you do use the Resources.Load()
however, you will need to keep a few things in mind. The path
is case-sensitive and requires you to add the file extension as well. So in the example above, that would result in:
myMaterial = Resources.Load("Materials/Night_Sky.mat");
As Nika Kasradze mentioned in the comments. In unity 5.0 or up extensions must be omitted. Making the correct unity5 syntax
myMaterial = Resources.Load("Materials/Night_Sky");