• 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 someguy004 · Apr 27, 2020 at 08:02 AM · camerarenderingmaterial

Materials appear missing at some distances away from the object

I have a script that generates voxel terrain and applies a random material to each cube.

The materials seem to be applied fine, but when any camera (both game camera and scene camera) moves to certain distances away, the objects appear to have no material set. If one camera causes it, it is only visible to that camera

I don't know if this is an error with unity, or with my script.

 // create a unit cube and store the mesh from it
 GameObject blockMesh = Instantiate(blockPrefab, Vector3.zero, Quaternion.identity);
 
 MeshFilter blockMeshMesh = blockMesh.GetComponent<MeshFilter>();
 
 // add mesh renderer component
 MeshRenderer mr = blockMesh.GetComponent<MeshRenderer>();
 
 // move the unit cube to the intended position
 blockMesh.transform.position = new Vector3(x, y, z);
 
 chooseMesh = Random.Range(0f, 1000f);
 
 // Randomly decide a material for the cube 
 // testing for different items with different rarities
 if (chooseMesh >= 0f && chooseMesh < 500f)
 {
     //set material to avoid evil pinkness of missing texture
     mr.material = material_1;
 }
 else if (chooseMesh >= 500f && chooseMesh < 750f)
 {
     mr.material = material_2;
 }
 else if (chooseMesh >= 750f && chooseMesh < 875f)
 {
     mr.material = material_3;
 }
 else if (chooseMesh >= 875f && chooseMesh < 937.5f)
 {
     mr.material = material_4;
 }
 else if (chooseMesh >= 937.5f && chooseMesh < 1000)
 {
     mr.material = material_5;
 }

The camera clipping planes are fine. The object is still rendered, but the material is not. It's not only near and far, materials turn pink briefly at regular intervals.

The code is in a Generate() function, which is called in Start()

I have spent a month trying to solve this and people on StackOverflow keep downvoting or locking my questions without answering them.

Comment
Add comment · Show 1
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 Namey5 · Apr 27, 2020 at 08:37 AM 0
Share

It's kind of hard to say without knowing your scene. One thing I will say however is that your last condition isn't quite right. Not only do you not have a default case (which you should), but you check to less than 1000 exclusive, even though 1000 can be an outcome - which would result in no material being set. I would suggest more something like this;

 ...
 else if (choose$$anonymous$$esh >= 875f && choose$$anonymous$$esh < 937.5f)
 {
     mr.material = material_4;
 }
 else
 {
     mr.material = material_5;
 }

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

217 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 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 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 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 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 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

Best way to swap between view modes 0 Answers

different material for multi camera 1 Answer

Render tooltip on top of everything with MRTK 0 Answers

LWRP: Set custom shader at runtime 1 Answer

Rendering objects into another camera with a different material in HDRP 0 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