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
0
Question by zombience · May 31, 2012 at 12:25 AM · arraymaterialsrendererdictionaryinstantiation

Missing Materials on build

I have 20 materials in an array that I am randomly choosing to apply to an object on instantiation. They appear fine when running the scene in the editor, but do not come through in the build.

I have tried both storing the Materials in an array (which results in magenta squares, i.e. missing materials) and also in a dictionary (which results in the default texture displaying, again, null materials).

Here are the code snippets I'm using. I'd appreciate any help with this, as I've been trying to sort this for a bit and can't seem to pin it down. I've tried moving the materials to different locations in the Project window under assets/materials or assets/resources/materials, and this doesn't seem to help.

   Public class Spawner : MonoBehaviour {
 
 private Dictionary<int, Material> matDictionary = new Dictionary<int, Material>();
     
     void Start () {
         getAllGalaxies();
     }
     
     void getAllGalaxies() { 
         allGalaxyMats = new Material[20]; 
         for(int i = 0; i < 20; i ++) { 
             Material galaxyMat = (Material) Resources.LoadAssetAtPath("Assets/Materials/Galaxy" + i + ".mat", typeof(Material)); 
             matDictionary.Add (i, galaxyMat);
                allGalaxyMats[i] = galaxyMat;
         }
     }
 
     void createFollowPoint() { 
         Vector3 tempLoc = new Vector3(0,0,0);
 
         GameObject galaxy = (GameObject) Instantiate(Resources.Load("TUIOGalaxy"));
         galaxy.transform.position = tempLoc;
         int tempInt = Random.Range (0, 19);
     
         Material tempMat = new Material(matDictionary[tempInt]);
         galaxy.renderer.material = tempMat;
     
         galaxy.renderer.material = allGalaxyMats[Random.Range (0, 19)];
     }
 }

I've included both the array and dictionary ways of doing things here, just so you might see any flaws in my thinking and help to point them out. Also, tempLoc is a variable that gets set elsewhere, but isn't relevant to illustrate my point.

Thank you in advance.

Comment
Add comment · Show 2
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 zombience · May 30, 2012 at 11:52 PM 0
Share

Also, I'm finding an odd side effect in switching to dictionary... I have a timer limiting how often these can spawn, and when a dictionary is being used and failing, the default material gets pulled up, and these spawn like crazy with little heed to the timer limit. If using an array (which still fails, but just displays magenta squares), the spawn timer is still heeded. I'm confused.

avatar image Googoo04 · Jan 30, 2017 at 02:14 AM 0
Share

Hi, I am having the same problem, except I am dragging the materials from the editor into the script before runtime. Any ideas or help?

1 Reply

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

Answer by zombience · Aug 23, 2012 at 08:19 PM

In case anyone ever finds this in the future and needs to know the answer to this, the solution was simple, and I'm disappointed no one suggested trying this. As a beginner, I realize there are many ways to do the same thing, but I don't know what they all are.

So, instead of doing

 for(int i = 0; i < 20; i ++) { 
      Material galaxyMat = (Material) Resources.LoadAssetAtPath("Assets/Materials/Galaxy" + i + ".mat", typeof(Material)); 
      matDictionary.Add (i, galaxyMat);
         allGalaxyMats[i] = galaxyMat;
    }



and basically loading the materials from Assets at runtime, it worked to create a public Materials array, and then just drag and drop all 20 materials into the array via the editor. Then, in the script, I access the materials array in the same way I did here in the script.

The difference is manually putting the materials in via editor vs. trying to load them from the assets folder at runtime.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Mesh Renderer Matereal Aray reduce? 0 Answers

How to check if the string is on a text file 0 Answers

Changing two different objects renderer colour 1 Answer

Emissive Material control via script (code) 1 Answer

Changing Color of Secondary Material in Materials Array 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