• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CloverKitsune · Apr 08, 2013 at 01:53 AM · c#errorfloatintegercast

Float to int casting in C# script with modulo giving division by zero error

I'm following this tutorial on using offsetting to animate a sprite sheet, which was going fine until I had to add controls for fps. The lesson is in JS, but I need to learn in C#. Unlike the example in the video, I have to explicitly cast the (Time.time * fps) from float to int, but no matter which method I try, it throws up a division by zero error on the line with the modulo. This makes little sense since print(frameNumber) shows nothing but integers. Does anyone know what's going on? I've been searching and searching, but can't find any answers

This is my code:

 public int columns;                // frames along x
 public int rows;                // y
 public int fps = 8;                // frames per sec
     
     void Update () 
     {
         int frameNumber = Time.time * fps; // Need to cast float to int here, but it causes error                    
             
         print (frameNumber);
         frameNumber = frameNumber % (columns * rows);                // ex. 1 % 16 = 1, 17 % 16 = 1. dividend = quotient * divisor + remainder, 1 = 0 * 16 + 1, 17 = 1 * 16 + 1.
         Vector2 size = new Vector2 (1.0f / columns, 1.0f / rows);     // ex. sheet has 16 frames across, scale texture to 1/16 width
         Vector2 offset = new Vector2 (frameNumber * size.x, rows);    // ex. (1 * 1/16, 1)
         
         renderer.material.mainTextureOffset = offset;                // offsets texture to other "frames"
         renderer.material.mainTextureScale = size;                    // scale texture to proper size


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Loius · Apr 08, 2013 at 02:40 AM

columns * rows is zero

% does actually perform a division operation, you can't pass zero into it

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CloverKitsune · Apr 08, 2013 at 05:47 AM 0
Share

Ah, I just figured it out and it was caused by a completely different set of zeros than I expected. I forgot that the variables for columns and rows needed to be filled in in the Inspector or else they turn up zeros.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Getting "Cannot cast from source type to destination type" even though it's working. 1 Answer

C# getComponent result error cast 3 Answers

Why are floats not accessing my inputted values? 1 Answer

Is There A Way To Cast On 1 Returning Value 2 Times? 5 Answers

How to i set the int to hitpoints / 10 + the current int. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges