Search Unity

update function slow down !

Discussion in 'Scripting' started by yogeez, Mar 31, 2009.

  1. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    hello all,

    I just want to know if i use Update() function attached on different game objects, will it hinder the performance of iphone. Because in pc the iphone simulation runs good. And vertices are also under 3.0 K as i know the counts should be under 10 K.
    So the build in iphone is working very slow and jerky, also i want to ask is
    Does transparent vertex lit and self illumination material causes performance lag ?

    my previous post is still not answered by anyone and so i am stuck with that part also.
    It will be helpful if some solution is provided.

    thanks regards
     
  2. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Check out the resources section there's a video form Unite 08 that talks about iphone and how achive good performance.

    I think the transparent shader might be the cause I recall they say something about those types of shaders.

    As far as the update function goes, just make sure you don't have any empty update functions as that will cause a little overhead.
     
  3. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    Thanks for the help
    And yaa i have already seen the unite 08 video for iphone and now as per my problem regarding slowdown.
    I also checked the self illumination and vertex lit for image texture used just simple texture in diffuse only mode.
    Also i want to tell is i have kept some 3 counter variable with conditions like if one is met the other is fired and so on, these are done on 2 of the game object under update function.
    Can this be the cause of slowdown.
    Or i am programming it in a wrong manner, Here but in the game view it works fine but not in iphone thats why i am asking.
     
  4. cyb3rmaniak

    cyb3rmaniak

    Joined:
    Dec 10, 2007
    Posts:
    162
    Well, this is easily checked.
    Just comment those Update() functions and run it without them. see if you get any performance gain.
    Just make sure to comment the entire function, and not just what's inside it.

    And about what you checked - did any of those things help?

    Also - the stats window is your friend. I'm not an iPhone programmer yet, but I imagine the iPhone version of Unity has one (should be in the game screen on the left upper corner). Check the amount of vertices, draw calls and textures you have when you play it inside the editor.
    Even if in the editor it runs fine, it doesn't really matter, since your computer's hardware is far stronger than the iPhone's.
    But the stats won't lie.
    I remember hearing somewhere how much vertices, textures and draw calls the iPhone can support and still give out decent frame rates, so look it up and try to stick to the recommended values...
     
  5. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    Yes i checked with texture on 3 of the small plane objects.

    Regarding the textures with self illumination transparency and vertex lit on another plane object i made it to simple diffuse but it was not the problem as by going to simple texture also the same slow down of animation and events is happening.

    Yaa checking stats is the proper way to make the programme efficient. So as per the iphone specs i have removed the heavy mesh and used the simple meshes to make it under 3K (previously goint about 13K), as per the use with iphone 10K is the max for vertices, textures and drawcalls. But still no effect on the output on iphone.

    I think there is something wrong with the programming, otherwise the design is not that much heavy as per the stats show. yaa i am using counter ++ which when hits the said limit the event is fired from one script to the other script which is in update function. Thats why i am using update as it can get the value from static boolean value at the right point of time from other scripts and perform the task to other function kept under the conditions.


    Want to ask simple thing, just want to know is it wrong to use update function on different game objects. And is there any way that i can stop it after its use and reuse it to run when i need.

    may be that can be the cause. If that is the thing i will have to change the whole objects and script scenario thats why asking if that can be the cause.

    And yaa i am using this script for showing and hiding the two plane objects with pics, one is with hit scene and the other is with normal one.

    gameObject.FindWithTag("pln1").renderer.enabled = false;
    gameObject.FindWithTag("pln2").renderer.enabled = true;

    i am switching it with the time delay function i made and the hit that is causing to enabled the hit pic viewable.

    I know lot of things in this reply but i am eager to know lots of stuff, thanks to all
     
  6. cyb3rmaniak

    cyb3rmaniak

    Joined:
    Dec 10, 2007
    Posts:
    162
    How much draw calls are you using? 30 is the number you should go for from what I've read on the forum...
    If your draw calls get higher than 30 that will be the problem...

    About the programming itself, try commenting out the update functions and run it on the iPhone. See if it helps. You should feel the performance difference if that is the cause of your problems.

    obviously Update() functions run every frame. I couldn't understand exactly what you're doing there, but unless you're using GameObject.Find() or heavy functions like Vector3.Magnitude() for every single frame, it should be fine. If all you're doing is:
    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.    counter++;
    5.    if (counter == 10000)
    6.    {
    7.       counter = 0;
    8.       Debug.Log("blah");
    9.    }
    10. }
    11.  
    Then you're fine.

    If you're doing something like:
    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.    counter++;
    5.    if (counter == 5)
    6.    {
    7.       GameObject.Find("blah").GetComponent("myScript").DoStuff();
    8.    }
    9. }
    10.  
    Then you might be having some performance issues in the code.

    But first thing's first - check the stats again and notice your draw calls. If you're way over 30 - that's probably your problem.
    If not - comment out the Update() function and all the code inside it, and run the game on the iPhone. See what happens if the code inside it doesn't run- will it solve the performance problem or not.

    About Update() functions - you can't cancel them completely in runtime. You can disable the script they are in, but that's about it.
    You can instead use coroutines (not 100% sure if they run on the iPhone, but I don't see why they wouldn't...):
    Code (csharp):
    1. function Start()
    2. {
    3.    Startcoroutine(MyUpdate());
    4. }
    5.  
    6. function MyUpdate()
    7. {
    8.    while (true)
    9.    {
    10.       yield WaitForSeconds(2.5);
    11.      
    12.       // Do your stuff
    13.    }
    14. }
    15.  
    And if you want to cancel that coroutine, use StopCoroutine

    Hope it helps...
     
  7. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    Yaa i checked the draw calls they are around 17, and textures and vertices are around 2 to 3K.

    As per the counter code my code is fine as per your example of 10000 counter. And as per your suggestion i will check about the coroutines and update function commenting and will check the results.

    I have checked the array thing also disabled it once to check on iphone but no solution. Hope i find a proper way, but i will post the updates on this...

    thanks
     
  8. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    Checked the Update function commenting thing, and found that it was Update function with counter ++ and functions calls that were causing the slow down problem.

    so i have done now with StartCoroutine like this
    Code (csharp):
    1.  
    2.  
    3. function rn1()
    4. {
    5. StartCoroutine("appear",2.0);
    6. yield WaitForSeconds(2);
    7. StopCoroutine("appear");
    8. }
    9.  
    10.  
    1. Here the problem is it runs for one time only, so if i need it for 15 times, I am keeping the three statements 15 times one below the other in rn1() function. Its not the right way but i am not able to fire it for a given number of times.

    2. Also i want to run the rn1() function when some static variable is set to true (saying static variable because the variale is passed a value in the other js file).

    Is there a way without using the update and fixedupdate function.

    thanks
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Make that thing itself a coroutine
    Put use While(true), if(staticvariable) and for(int i=0;i<15, i++) and yield WaitForSeconds(1);

    the last part is within the while, after the if block and ensures that the coroutine does not bomb the app with uneeded checks.
     
  10. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    yaa dreamora, you are right i think i am not getting the proper way to set it exactly the way you defined, can u pls refine it a little.

    still i will post the updates as the code works out.

    thanks