Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

For loop

Discussion in 'Scripting' started by tawdry, May 18, 2018.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,122
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Texturescale : MonoBehaviour {
    6.     public float a = 1f;
    7.     public float b = 1f;
    8.     //public int i=0;
    9.     public Renderer rend;
    10.     void Start() {
    11.         rend = GetComponent<Renderer>();
    12.  
    13.     }
    14.     void OnTriggerEnter(){
    15.         for (int i = 0; i < 1000; i++) {
    16.             a = a + 0.0001f;
    17.             b = b + 0.0001f;
    18.             float scaleX = a;
    19.             float scaleY = b;
    20.             rend.material.mainTextureScale = new Vector2 (scaleX, scaleY);
    21.         }
    22.     }
    23. }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,084
    Yes there is. Put the for loop inside a Coroutine. :)
     
  3. oLDo

    oLDo

    Joined:
    Mar 14, 2017
    Posts:
    39
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,891
    Yes, Coroutine or look into a tweening engine.
     
  5. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,122
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Texturescale : MonoBehaviour {
    6.     public float abra=7.0f;
    7.     public float bbra=7.0f;
    8.     //public int i=0;
    9.     public Renderer rend;
    10.     void Start() {
    11.         rend = GetComponent<Renderer>();
    12.        }
    13.     void OnTriggerEnter ()
    14.     { StartCoroutine ("Scale");
    15.        
    16.     }
    17.  
    18.         IEnumerator Scale() {
    19.        
    20.         for (int f = 1; f < 100000000; f=f+1) {
    21.             abra = abra + 0.0001f;
    22.             bbra = bbra + 0.0001f;
    23.             //Debug.Log (abra);
    24.             float scaleX = abra;
    25.             float scaleY = bbra;
    26.             rend.material.mainTextureScale = new Vector2 (scaleX, scaleY);
    27.                 yield return null;
    28.             }
    29.         }
    30.     }
    31.  
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    1,572
    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.