Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Puzzling question

Discussion in 'Scripting' started by omglazerz, Mar 21, 2013.

  1. omglazerz

    omglazerz

    Joined:
    Feb 16, 2013
    Posts:
    4
    Hello to the Unity community first time poster here! I have been going through the Walker Boys tutorials learning Unity development and I am kind of stumped as to why this is happening. I am on the tool development tutorial and during step 6 of the ani tool spreadsheet creating a 2d array offset and this is my code(C#) which works fine.;

    public class step62darrayoffset : MonoBehaviour
    {

    public int column; //u coordinate
    public int row; //v coordinate
    public float framesPerSecond = 8.0f;


    void Update()
    {
    int index = Mathf.RoundToInt(Time.time * framesPerSecond); //Time control FPS.

    index = index % (column * row); //End of the row(modulated).
    Vector2 size = new Vector2(1.0f / column, 1.0f / row); //scale.


    int u = index % column;
    int v = index / column;

    Vector2 offset = new Vector2(u * size.x, (1 - size.y) - (v * size.y)); //offset.
    print(index);
    renderer.material.mainTextureOffset = offset; //texture offset.
    renderer.material.mainTextureScale = size; //texture scale.

    }
    }


    But when i move the (u) and (v) variables to initialize them just after the index initialization when i run the animation it works for a few frames and then changes to a bunch of lines on the animation spreadsheet, like its changing the scale or something. Now i don't mind but i usually like to initialize variables at the top if i can and don't see why it would make a difference.

    Any explanation to this issue would be much appreciated!
     
  2. omglazerz

    omglazerz

    Joined:
    Feb 16, 2013
    Posts:
    4
    bump! nobody?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    first off, [code ] [/code ] tags make things much more readable (and gives line numbers :) )

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     int index = Mathf.RoundToInt(Time.time * framesPerSecond); //Time control FPS.
    5.  
    6.     index = index % (column * row); //End of the row(modulated).
    7.     Vector2 size = new Vector2(1.0f / column, 1.0f / row); //scale.
    8.  
    9.  
    10.     int u = index % column;
    11.     int v = index / column;
    12.  
    13.     Vector2 offset = new Vector2(u * size.x, (1 - size.y) - (v * size.y)); //offset.
    14.     print(index);
    15.     renderer.material.mainTextureOffset = offset; //texture offset.
    16.     renderer.material.mainTextureScale = size; //texture scale.
    17.  
    18. }
    19.  
    do you move lines 9 and 10 to just after line 3? (i.e. before line 5 where the index is updated...)
     
  4. omglazerz

    omglazerz

    Joined:
    Feb 16, 2013
    Posts:
    4
    Yes. And thank you for replying and the tip on the /code i will use that next time.
     
  5. omglazerz

    omglazerz

    Joined:
    Feb 16, 2013
    Posts:
    4
    And i think you answered my question thanks!