How to avoid Division by zero with this code?

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

Take a look through your code - the only time that you’re dividing (and therefore the only time you’ll come across a divide by zero error) is when you’re dividing by your variables for row and column count and those values are zero. I’m guessing that this is a mistake, so make sure that you always have at least one row and one column defined. If this isn’t always going to be the case, you’ll need to add logic (switches, if/then, etc.) to bail out in those cases.

Good luck.

totalCells can’t be zero.

index % totalCells => index – (index / totalCells) * totalCells

“index” CAN be zero, it can also be negative, and it even doesn’t have to be integer.

0 % 5 == 0

In your script “totalCells” is zero by default. Either you hasn’t changed its value in Inspector, or it is zeroed elsewhere. I see no other reason the %-operator can throw DivideByZero exception.