- Home /
Sharing texture a cross multiple objects?
I am trying to during run-time change the texture on an object. But I am having issues. When I only have one object. The animation is great! but as soon as i introduce a 2nd object, they seem to get confused with which texture to use?
This is the script that gets called in LateUpdate
public void UpdateImage(Texture2D pTexture, Vector2Int pSpriteSize)
{
if (spriteMesh == null)
return;
if(spriteMesh.texture != pTexture)
spriteMesh.texture = pTexture;
int uvTileX = (int)pTexture.width / (int)pSpriteSize.x;
int uvTileY = (int)pTexture.height / (int)pSpriteSize.y;
// Calculate index based on FPS and our timeScale
int _index = (int)(Time.timeSinceLevelLoad * fps) % (pSpriteSize.x * pSpriteSize.y);
// split into horizontal and vertical index
int uIndex = 0;
//This will create an instant character image switch when the player rotates the player around the player.
// There used to be a noticable pause in changing the image
if(forceAnimate == false)
if(prevIndex == _index)
return;
//Are we animating? If so then update the uIndex
if(IsAnimating == true)
{
uIndex = _index % uvTileX;
forceAnimate = IsAnimating;
}
//If at anypoint our forceAnimate is false, do NOT update the mesh
if(forceAnimate == false)
return;
forceAnimate = false;
prevIndex = _index;
renderer.sharedMaterial.mainTexture = pTexture;
meshFilter.mesh=null;
meshFilter.mesh=spriteMesh.DrawMesh3D(new Vector2Int((uIndex)* pTexture.width/ uvTileX, (3-(int)currentDirection)* pTexture.height/ uvTileY), new Vector2Int(pTexture.width/ uvTileX, pTexture.height/ uvTileY));
}
The mesh renders just fine. Its the texture on the material that seems to be screwing up.
capture.png
(38.5 kB)
Comment
Answer by KitoCode · Mar 23, 2020 at 02:22 PM
renderer.sharedmaterial.mainTexture = character.CurrentState.SpriteSheet; duh.... ooops
solution : renderer.material.mainTexture = character.CurrentState.SpriteSheet;
Your answer
![](https://koobas.hobune.stream/wayback/20220612225544im_/https://answers.unity.com/themes/thub/images/avi.jpg)