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 /
This question was closed May 09, 2015 at 10:22 PM by joshua-lyness for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by joshua-lyness · May 06, 2015 at 08:42 PM · arraymaterialrandomrendererrange

renderer.material doesnt work

renderer.material doesn't work if you use arrays? how come? and how do you get around it?

my code:

 public class treeSettings : MonoBehaviour 
 {
     public Material[] myMaterials;

 
     void Start () 
     {
         gameObject.GetComponent<Renderer>().material = myMaterials[Random.Range(0,myMaterials.Length)];
     }
 }




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

  • Sort: 
avatar image
2
Best Answer

Answer by Ilgiz · May 06, 2015 at 09:41 PM

Everything works fine. I checked out

try this

 gameObject.GetComponent<MeshRenderer>().material = myMaterials[Random.Range(0, myMaterials.Length)];

or

check the variable myMaterials in inspector

Comment
Add comment · Show 8 · 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 joshua-lyness · May 06, 2015 at 10:09 PM 0
Share

am I meant to store the material array in the variable my$$anonymous$$aterials of my script, or as an array in the meshRenderer? :) Ive tried both, but it doesn't work... here is a screenshot of what happens: alt text

screenshot-26.png (244.5 kB)
avatar image Ilgiz · May 07, 2015 at 08:40 AM 0
Share

hm.. try to use boxes instaed your trees.

avatar image joshua-lyness · May 07, 2015 at 09:14 AM 0
Share

still doesn't work.... :( What about if I change the texture attached to a material? so using an array of texture, randomly select one and replace the current material... ahh this is annoying... :/ must be doing something seriously wrong...

avatar image Ilgiz · May 07, 2015 at 10:04 AM 1
Share

"When you assign the material by hand on your tree does it render as expected ? " answer to the good question by 0potable

avatar image joshua-lyness · May 07, 2015 at 12:19 PM 0
Share

yea it does... which is the weird thing....

Show more comments
avatar image
3

Answer by Rtyper · May 07, 2015 at 03:48 PM

I can see a Mesh Renderer component on your tree GameObject, but no Mesh Filter, so it has nothing to render. I don't know for sure, but I'd bet the correct mesh renderer (if there's only 1 mesh) is on a child GameObject.

Look on the child objects in your prefab. If there's just one with both a Mesh Filter and a Mesh Renderer component on, then put your Tree Settings script on that instead and it should work.

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 joshua-lyness · May 07, 2015 at 04:22 PM 0
Share

So I just need to add a mesh filter to all of the clones? Like add it to the prefab?

avatar image 0potable · May 07, 2015 at 04:34 PM 0
Share

Yes. The meshFilter component store the mesh data and the meshRenderer store the material (how the mesh will render).

avatar image joshua-lyness · May 07, 2015 at 06:25 PM 0
Share

I don't know whether you can see my answer below.... I was wrong. You were right, I needed a mesh filter! thanks :) sorted. ahhh, I can breathe now.

Oh and also, I didn't realise, but when I made my prefab, I had a prefab within a prefab.... this meant that both of those prefabs have to have the same materials and components etc. I deleted the children prefabs, and voila! It works..... thanks everyone that helped me... guess it was just me being an idiot.... :D

avatar image
2

Answer by 0potable · May 07, 2015 at 09:50 AM

Does your tree have correct UV's ?

It's seems that your shader render just fine on your material (as seen on your inspector).

When you assign the material by hand on your tree does it render as expected ?

Edit : I'm asking this question because your script seems to work just fine.

Maybe your problems is somewhere else :)

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 joshua-lyness · May 07, 2015 at 10:35 AM 0
Share

yea, the UV's definitely work, since if I add the material normally, it works fine. I made it in blender and imported it, with uv's too, so Im sure that they work.

When I get back home later I will try making a scene from scratch and importing the script, since like you said, Im sure it should work...

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Changing two different objects renderer colour 1 Answer

Random Range Seems... Unrandom 1 Answer

Load, change material to game object II (not a repeat) 1 Answer

How to know what random number is chosen 2 Answers

Store multiple random integers in an array? 4 Answers


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