Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Tarikm · Jan 21, 2015 at 02:16 PM · materialloadskybox

Load material from assets

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by DNRN · Jan 21, 2015 at 02:49 PM

You need to put your materials in a parent folder named Resources like this: "Resources/Material/Night_Sky.mat"

Just to summerize sniper43s comment, here is an complete example:

  Resources.Load("Material/Night_Sky.mat", typeof(Material)) as Material;

Then Unity is aware :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sniper43 · Jan 21, 2015 at 11:32 PM 0
Share

Gettign resources through code like so

 Resources.Load("$$anonymous$$aterial/Night_Sky.mat", typeof($$anonymous$$aterial)) as $$anonymous$$aterial;

is very processor heavy, and should be avoided. YOu can ins$$anonymous$$d assign an inpector object fromt eh resources folder (Objects don't have to be in-scene)

avatar image i3130002 · Jun 05, 2018 at 03:05 PM 0
Share

Does not work on Unity 2017

avatar image metroidsnes · Jun 25, 2018 at 09:35 AM 1
Share

Use can use also Resources.Load<$$anonymous$$aterial>("Night_Sky"); $$anonymous$$aterial name cannot contain file extension.

avatar image
3

Answer by sniper43 · Jan 21, 2015 at 02:50 PM

It should be better to have a public Material NightSky at teh begining of your script, and drag and dorp it in the inspector.

You found one of the ONLY ways to load materials from code, but it is usually not recommended.

To switch the skyboxes material modify it's renderer.material property

http://docs.unity3d.com/ScriptReference/Renderer.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Obree · Sep 19, 2016 at 04:58 PM

If you wanted to load and set of Materials into an array you could use

 Material[] mats = Resources.LoadAll("Materials", typeof(Material)).Cast<Material>().ToArray(); 

With the materials saved in Assets/Resources/Materials folder.

I had some trouble getting the cast right so thought I would put if up for anyone stuck like me

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aledend · Dec 25, 2018 at 05:17 PM 0
Share

That would simply be:

 $$anonymous$$aterial[] mats = Resources.LoadAll<$$anonymous$$aterial>("$$anonymous$$aterial/");
avatar image
1

Answer by Cursedth · Oct 05, 2017 at 04:12 PM

It works. But you have to leave out the ".mat" extension

 material = Resources.Load("/Materials/mymaterial", typeoff(Material)) as Material;

Where "mymaterial" is the material you want to load.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image i3130002 · Jun 05, 2018 at 03:05 PM 0
Share

Does not work on Unity 2017

avatar image Willard720 · Aug 24, 2018 at 04:16 PM 0
Share

Fix your grammar... jeez... Also, it's typeof not typeoff

avatar image
1

Answer by astracat111 · Aug 05, 2018 at 12:27 PM

I think it has something to do with the path name, not with Resources.Load. As metroidsnes said, you can't have .mat. Still trying myself, will post some results.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fewpwew130 · Jul 04, 2019 at 10:35 AM 0
Share

In my case there was an error "Resources/$$anonymous$$aterials/Green_grass", ins$$anonymous$$d it should have been "/$$anonymous$$aterials/Green_grass" So as it is written in the documentation, the initial folder name (Resources) should not be in the path.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Skybox showing mostly white 0 Answers

Environment Material Disappears after ingame level restart 0 Answers

change skybox via script help ? 1 Answer

Changing two different objects renderer colour 1 Answer

Creating Material from String (Unity 5.6) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges