Hi Just experimenting with texture scaling using a for loop but it doesn't gradually show the changes and only updates the texture at the end of the loop.. .Also the system waits for the for loop to complete before it continues is there a better command that will allow other processes to run while the scaling code is executing? Code (CSharp): using UnityEngine; using System.Collections; public class Texturescale : MonoBehaviour { public float a = 1f; public float b = 1f; //public int i=0; public Renderer rend; void Start() { rend = GetComponent<Renderer>(); } void OnTriggerEnter(){ for (int i = 0; i < 1000; i++) { a = a + 0.0001f; b = b + 0.0001f; float scaleX = a; float scaleY = b; rend.material.mainTextureScale = new Vector2 (scaleX, scaleY); } } }
Thx guys now know how to do coroutines but there are a couple things i noticed that worry me a bit. When I first wrote the code i had a and b equal to 1; Later I changed that to them been equal to 7 but the script never recognised this. I then changed the letters by putting bra in front of them and again setting the value to 7 this time the value in the inspector showed 0.I eventually removed the script from the object and reapplied it then the value showed as 7. Is this somewhat normal behavior for unity? that a script will ignore changes to it unless it is removed and reapplied to the object. I haven't seen this before. Also if i add to many zero's to the a+a=0.00001 line it no longer seems to work. Here's the working script Code (CSharp): using UnityEngine; using System.Collections; public class Texturescale : MonoBehaviour { public float abra=7.0f; public float bbra=7.0f; //public int i=0; public Renderer rend; void Start() { rend = GetComponent<Renderer>(); } void OnTriggerEnter () { StartCoroutine ("Scale"); } IEnumerator Scale() { for (int f = 1; f < 100000000; f=f+1) { abra = abra + 0.0001f; bbra = bbra + 0.0001f; //Debug.Log (abra); float scaleX = abra; float scaleY = bbra; rend.material.mainTextureScale = new Vector2 (scaleX, scaleY); yield return null; } } }
If you have a public variable that defines a default value, Unity will save that value with the object. If you change the default value in script, only newly created objects will use the new default value. Imagine the chaos if you changed a default value and all of your objects lost their values. As for your script, you're not using time based values at all, so it's entirely framerate dependent. You should be scaling your values by delta time.