- Home /
Auto Texture Tiling Script Problem
I want to write a script that will allow a texture's tiling variables to be derived from the scale of the object. Like this:
transform.renderer.material.mainTextureScale = new Vector2( this.gameObject.transform.lossyScale.x/scale, this.gameObject.transform.lossyScale.y/scale );
That works fine at runtime, but as soon as I try to get things updating in the editor I hit problems.
This yield's an error:
void Update()
{
if( !Application.isPlaying )
{
transform.renderer.material.mainTextureScale = new Vector2( this.gameObject.transform.lossyScale.x/scale, this.gameObject.transform.lossyScale.y/scale );
}
}
"Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead. UnityEngine.Renderer:get_material()"
This doesn't have an error, but if you duplicate the object then scaling one object will affect the tiling for both. Unity will also start complaining about leaked objects.
void Update()
{
if( !Application.isPlaying )
{
transform.renderer.sharedMaterial.mainTextureScale = new Vector2( this.gameObject.transform.lossyScale.x/scale, this.gameObject.transform.lossyScale.y/scale );
}
}
"Cleaning up leaked objects in scene since no game object, component or manager is referencing them"
Is there a way to do what I want?
Your answer
Follow this Question
Related Questions
Reimporting during play changes UVs/Tiling? 0 Answers
Unknown EditorApplication.SaveScene() errors 1 Answer
Modify texture coordenates with a shader. 0 Answers
Script runs in editor, on its own 0 Answers
blender uv to image editor 0 Answers