# Puzzling question

**URL:** https://discussions.unity.com/t/puzzling-question/500368
**Category:** Scripting
**Tags:** Scripting
**Created:** [March 21, 2013, 7:15pm UTC](https://discussions.unity.com/t/puzzling-question/500368 "2013-03-21T19:15:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![omglazerz](https://avatars.discourse-cdn.com/v4/letter/o/6a8cbe/32.png) [@omglazerz](https://discussions.unity.com/u/omglazerz)
#### Post date: [March 21, 2013, 7:15pm UTC](https://discussions.unity.com/t/puzzling-question/500368/1 "2013-03-21T19:15:49Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![omglazerz](https://avatars.discourse-cdn.com/v4/letter/o/6a8cbe/32.png) [@omglazerz](https://discussions.unity.com/u/omglazerz)
#### Post date: [March 21, 2013, 11:04pm UTC](https://discussions.unity.com/t/puzzling-question/500368/2 "2013-03-21T23:04:25Z")

</div>

bump! nobody?

---

<div class="post-metadata">

### Author: ![LeftyRighty](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@LeftyRighty](https://discussions.unity.com/u/LeftyRighty)
#### Post date: [March 22, 2013, 12:14am UTC](https://discussions.unity.com/t/puzzling-question/500368/3 "2013-03-22T00:14:01Z")

</div>

first off, [code] [/code] tags make things much more readable (and gives line numbers 🙂 )

```auto
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.

}

```

do you move lines 9 and 10 to just after line 3? (i.e. before line 5 where the index is updated…)

---

<div class="post-metadata">

### Author: ![omglazerz](https://avatars.discourse-cdn.com/v4/letter/o/6a8cbe/32.png) [@omglazerz](https://discussions.unity.com/u/omglazerz)
#### Post date: [March 24, 2013, 5:11am UTC](https://discussions.unity.com/t/puzzling-question/500368/4 "2013-03-24T05:11:34Z")

</div>

> [@LeftyRighty](#):
>
> first off, [code] [/code] tags make things much more readable (and gives line numbers 🙂 )
> 
> ```auto
> 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.
> 
> }
> 
> ```
> 
> do you move lines 9 and 10 to just after line 3? (i.e. before line 5 where the index is updated…)

Yes. And thank you for replying and the tip on the /code i will use that next time.

---

<div class="post-metadata">

### Author: ![omglazerz](https://avatars.discourse-cdn.com/v4/letter/o/6a8cbe/32.png) [@omglazerz](https://discussions.unity.com/u/omglazerz)
#### Post date: [March 24, 2013, 5:12am UTC](https://discussions.unity.com/t/puzzling-question/500368/5 "2013-03-24T05:12:35Z")

</div>

And i think you answered my question thanks!
