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
1
Question by Kruga · Aug 04, 2010 at 06:44 AM · gameobjecttexturematerialload

Load Material to GameObject

Hi there,

I'm trying to load a material that has a Bumpmap added to it. If it can't find the material for the object, then it'll look for just a basic Texture2D texture.


try { sphere.renderer.material = Resources.Load("Planets/" + PlanetType.ToString()+".mat") as Material; if (sphere.renderer.material == null) throw new Exception(); } catch (Exception) { sphere.renderer.material.mainTexture = Resources.Load("Planets/"+PlanetType.ToString()) as Texture2D; }

     if (Texture != null)
     {
         sphere.renderer.material.mainTexture = Texture;
     }


Now it's not working. It's just loading an empty material in there. I rechecked the material to make sure that I didn't save it incorrectly. But it shows that the textures are still there. I checked the Material property to make sure it not read only. but it isn't..

So what am I doing wrong here?? If this will not work, how would I go about doing this.

Files are :

  • Acura.png = texture
  • Acure_BM.png = bumpmap
  • Acure.mat = material (including all bumpmap and information)

Thanks

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

2 Replies

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

Answer by Mike 3 · Aug 04, 2010 at 08:46 AM

The material load is wrong for sure - you can't add .mat on the end of it. If you have two items with the same name but different types, use the overload of Resources.Load which takes a second parameter for the type.

On top of that, you're currently assigning a null material to the renderer, throwing an exception, then using the null material as if nothing happened.

If it were me, I'd do something more like this:

Material mat = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Material)) as Material; if (mat != null) { sphere.renderer.material = mat; Texture2D tex = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Texture2D)) as Texture2D;

 if (tex != null)
     sphere.renderer.material.mainTexture = tex;

} else if (Texture != null) sphere.renderer.material.mainTexture = Texture;

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 Kruga · Aug 04, 2010 at 11:05 AM 0
Share

That seems better, but it still seems to be doing the same thing, of loading empty materials to the spheres. If I comment out the texture2d part the material loads fine, if I comment out the material code, the texture loads fine... But just does not seem to like both. I'll be moving to making materials for all at some point. I just want to keep it so, if I havn't made a material, It'll fall on texture2d.

That other 'if (Texture != null)' is just so if I assign something in the unity editor, it'll overload the rest of this and just load what I picked in the editor. I should have left it out.

avatar image Mike 3 · Aug 04, 2010 at 05:18 PM 0
Share

Ah I see, will update the example then

avatar image Mike 3 · Aug 04, 2010 at 05:20 PM 0
Share

That should load both the material and texture, and use the fallback if the material couldn't be found

avatar image
0

Answer by GeorgeAmos · Jan 28, 2014 at 01:43 AM

 try
 {
   Material mat = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Material)) as Material;
   if (mat != null)
   {
      sphere.renderer.material = mat;
   }
   else // mat is null, load the texture
   {
      Texture2D tex = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Texture2D)) as Texture2D;
      
      if (tex != null)
         sphere.renderer.material.mainTexture = tex;
   }
 
 }
 catch (Exception)
 {
    sphere.renderer.material.mainTexture = Resources.Load("Planets/" + PlanetType.ToString(), typeof(Texture2D)) as Texture2D;
 }
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

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

1 Person is following this question.

avatar image

Related Questions

Select colour then change texture 0 Answers

Material doesn't have a color property '_Color' 4 Answers

Can see 3D object through itself 0 Answers

How to make emissive maps glow? 2 Answers

Adding Texture image to gameobject 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