I’m using the SpriteSheetAdvanced code from the wiki, but I’m getting a “DivideByZeroException: Division by zero” error on this line:
// Repeat when exhausting all cells
index %= totalCells;
Here’s the full code bellow:
using UnityEngine;
public class SpriteSheetAdvanced : MonoBehaviour {
// Renderer material to blink if a renderer isn't attached to this gameObject
public Renderer[] renderers;
// Cell size (for uv display)
public bool autoCellSize = true;
public Vector2 customCellSize;
// Columns and rows
public int columnCount = 4;
public int rowCount = 4;
// Animation fields
public int rowNumber = 0; // Which row to start on
public int columnNumber = 0; // Which column to start on
public int totalCells = 0; // How many cells to animate through
public int framesPerSeconds = 10; // How fast to play the animation
private Vector2 cellSize;
private int lastIndex = -1;
private void Start () {
// Size of cells
cellSize = new Vector2( 1.0f / columnCount, 1.0f / rowCount );
if( this.renderer == null && renderers == null )
{
Debug.LogError("No renderer attached to this gameObject! " +
"No renderer assigned to 'renderers'!" +
"Make sure there's a renderer attached or specified or this script won't work.");
enabled = false;
}
}
private void Update () {
// Calculate index
int index = (int)( Time.time * framesPerSeconds );
// Repeat when exhausting all cells
index %= totalCells;
if( index != lastIndex )
{
// split into horizontal and vertical index
int uIndex = index % columnCount;
int vIndex = index / columnCount;
// Which cellSize to use - Auto or custom?
Vector2 texCellSize = autoCellSize ? cellSize : customCellSize;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
Vector2 offset = new Vector2(( uIndex + columnNumber ) * cellSize.x,
( 1.0f - texCellSize.y ) - (vIndex + rowNumber) * cellSize.y );
// Apply offset and scale to each material's texture
if( this.renderer != null )
{
renderer.material.SetTextureOffset( "_MainTex", offset );
renderer.material.SetTextureScale ( "_MainTex", texCellSize );
}
else
{
foreach( Renderer r in renderers )
{
r.material.SetTextureOffset( "_MainTex", offset );
r.material.SetTextureScale ( "_MainTex", texCellSize );
}
}
lastIndex = index;
}
}
}
How can I change it in order to get rid of the DividedByZero error?
Thanks a lot for any help!
Stephane