- Home /
Materials disappear when the camera is at certain distances away
I have a script that generates terrain through perlin noise by duplicating and moving a cube. All the materials are set fine, but when a camera moves to certain distances away from a cube, it appears as if there is no material. This doesn't appear on any other cameras, and also works with the scene camera as well as the game camera. I don't know if this is an error with unity or my script.
GameObject blockMesh = Instantiate(blockPrefab, Vector3.zero, Quaternion.identity);//create a unit cube and store the mesh from it
MeshFilter blockMeshMesh = blockMesh.GetComponent<MeshFilter>();
MeshRenderer mr = blockMesh.GetComponent<MeshRenderer>();//add mesh renderer component
blockMesh.transform.position = new Vector3(x, y, z);//move the unit cube to the intended position
chooseMesh = Random.Range(0f, 1000f);
//Randomly decide a material for the cube - testing for different items with different rarities
if (chooseMesh >= 0f && chooseMesh < 500f)
{
mr.material = material_1;//set material to avoid evil pinkness of missing texture
}
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;
}
Your answer
Follow this Question
Related Questions
Projecting camera view on to a right angled surface 1 Answer
How to make two different cameras view a material in two different colors? 0 Answers
How to maintain sharp lines (pixel-like effect) from small textures on 3D assets 2 Answers
How to make camera position relative to a specific target. 1 Answer
Render objects with different materials when switching between two cameras in HDRP 0 Answers