Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Fdudka · Dec 23, 2015 at 12:11 PM · materialrandomresources.loadall

Set Random Material from folder

Hello, I'm making an underground system made of Capsules and I have around 100 Materials all originate from Sprites texture. Eventually, there would be around 500 Capsules to cover a large area.

My goal, Give a different random Material from a folder to each Capsules at Start-Runtime.

-All the Materials are in a Folder named FirstLayerMaterials that is itself in a Folder named UnderGround.

-All the Capsules have the Script UnderGroundCapsules.

-All the Capsules are children of a GameObject named UnderGroundBlocks with the Script UnderGroundBlocks.

-All the Capsules have a tag "NotDurt".

-All the Capsules Shader must be set to Sprites/Default.

In C# in the Parent UnderGroundBlocks (Script UnderGroundBlocks) that holds the Capsules.

public class UnderGroundBlocks : MonoBehaviour {

 GameObject[] Durts;
 
 void Start(){
             Material[] materials = (Material[]) Resources.LoadAll("FirstLayerMaterials");
     Durts = GameObject.FindGameObjectsWithTag("NotDurt");
     foreach(GameObject capsule in Durts){
         capsule.GetComponent<Renderer>().material = materials[Random.Range(0, 100)];
     }
 }

}

My problem: Its giving an Error: InvalidCastException: Cannot cast from source type to destination type. UnderGroundBlocks.Start () (at Assets/WorldObject/UnderGound/UnderGroundBlocks.cs:13)

alt text

Thank you

screen-shot-2015-12-23-at-10440-pm.png (517.7 kB)
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

3 Replies

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

Answer by wibble82 · Dec 23, 2015 at 12:28 PM

Hi

To answer for certain, I'd need to know which line 'line 13' is (i.e. the line the error refers to).

I suspect it is this line:

 Material[] materials = (Material[]) Resources.LoadAll("FirstLayerMaterials");

The documentation seems to imply this is fine, but I'm fairly certain it isn't, as Resources.LoadAll won't necessarily return an array of materials. In fact I thought it would return a simple array of objects, possibly unless you actually specify a type as the 2nd parameter of Resource.LoadAll.

You could try:

     Material[] materials = (Material[]) Resources.LoadAll("FirstLayerMaterials", typeof(Material));
 

From the docs that looks like it might work, though I've not tried it.

Or if not, this should certainly work:

 void Start()
 {
     //allow Resource.LoadAll to just return a list of objects (filtered to only have materials in)
     Object[] materials = Resources.LoadAll("FirstLayerMaterials", typeof(Material));
 
     //get our 'Durts' and iterate over each one
     Durts = GameObject.FindGameObjectsWithTag("NotDurt");
     foreach(GameObject capsule in Durts)
     {
         //set material, adding a static cast to the Material type
         capsule.GetComponent<Renderer>().material = (Material)materials[Random.Range(0, 100)];
     }
  }

Though finally, to be on the safe side, use the materials array length instead of '100' in the random:

         capsule.GetComponent<Renderer>().material = (Material)materials[Random.Range(0, materials.Length)];
 

Hope that helps

-Chris

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
0

Answer by Fdudka · Dec 23, 2015 at 08:04 PM

Thank you Chris, yeah that was the line 13... Almost works, now its saying :

IndexOutOfRangeException: Array index is out of range. UnderGroundBlocks.Start () (at Assets/WorldObject/UnderGound/UnderGroundBlocks.cs:15)

line 15 is : capsule.GetComponent().material = (Material)materials[Random.Range(0, materials.Length)];

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 Fdudka · Dec 25, 2015 at 10:28 AM 0
Share

FirstLayer$$anonymous$$aterial folder was not in Resources folder....

Thank you and $$anonymous$$erry Christmas!!

avatar image
0

Answer by zoppytevo · Aug 14, 2020 at 02:44 PM

How do I make the material change once every 3 seconds or more?

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

37 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to have a scrolling background quad cycle through tiled materials? 0 Answers

Unparalleled problem with if-statement 0 Answers

Shader change works, but no texture - Android 0 Answers

Create a prefab from an instantiated object in the editor 1 Answer

Storing Renderer in a variable not working! 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