Search Unity

Unity hangs when i run this code

Discussion in 'Scripting' started by tawdry, Sep 30, 2014.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Hi I'm really struggling with the logic of c# I asked some help to get animated gifs to run but no replies so thought i'd give it a go.But seems i'v created a infinite loop and unity hangs. Heres the code please please help. I just want to cycle through 10 images in sequence.rinse and repeat.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class gifanim : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         int frames = 1010;//named the images 1000-1009 so i could increment them easily
    9.         for (int next=1000; next<frames; Debug.Log (next), next=next+1)
    10.                         if (next >= 1009)//last frame
    11.                                 next = 1000;//want to keep it animating so return to start of clips.
    12.  
    13.         }
    14. }
    So basically i don't know yet how to add the textures to the object or how to get the images to run only when they are in range of the camera or set up a time delay.(admit to been a c#dummy)(if u feel like helping me out on those as well i wont protest:D)

    Just want to be able to cycle through them when i do know how to do that without it hanging up the system.
    edit Tried putting it in update and did the same thing
     
    Last edited: Sep 30, 2014
  2. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    You've heard the one where the guy goes to the doctor and says, "it hurts whenever I do this" and the doctor says, "then don't do that?"

    Look into Update or UpdateFixed instead of Start. They are run over and over, you can use that instead of the loop.

    In this particular case, also look into the Animator component, and the SpriteRenderer. You can import a texture as a Multiple Sprite, using a sprite sheet of the different frames of your animation, then using the Animator and an animation clip, you can change the Sprite value on the SpriteRenderer. It may be easier to learn the Animator on some simpler properties like position and then come back to the SpriteRenderer once you're comfortable with that.
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Code (CSharp):
    1. int frame = 0;
    2. void Update(){
    3. frame += 1;
    4. //Show frame
    5.  
    6. //If we just showed frame 10. Go back to first
    7.      if (frame == 10){
    8.           frame = 0;
    9.      }
    10. }
     
  4. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Thx for replies yes thought update would work but i want to be able to set condition for it to run such as only when in view range would i be able to set those in update so it would only run animation when the conditions are met(want to try for best possible performance).
    yea the sprite stuff goes right over my head its like you speaking a foreign language it seems much easier to break up the gif and import the seperate files then run in a loop.

    What is the best way for a stationary scenic object to detect when a player is close enough to see it, raycast or is there some kind of vector3 comparison command?

    Does writing the code like this improve performance?
    1. frame += 1; // or is it the same as frame=frame+1 i'm less likely to make or spot an error writing it like this .
     
    Last edited: Sep 30, 2014
  5. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Vector3.distance :)

    Code (CSharp):
    1.  
    2. void Update(){
    3. Vector3.Distance(otherObject.transform.position,playerObject.transform.position);
    4. if(distance < 5){
    5.  
    6.     frame += 1;
    7.     //Show frame
    8.     //If we just showed frame 10. Go back to first
    9.          if (frame == 10){
    10.               frame = 0;
    11.          }
    12.  
    13. }
    14. }
     
  6. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    cool thx Are otherObject and playerObject the names i change or are they unity preset commands as in can i change them to whatever I need to. Also i have 10 textures named 0 to 9 (frame) that i want to use to change the texture on a created object called "water(a created plane)"
    So when it loops i want the texture to =frame(0 bmp to 9 bmp) . How do i change the texture of this "water" within the script?
     
  7. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490

    You'd have to assign them as public GameObject variables.

    public GameObject otherObject;
    public GameObject playerObject;

    Then assign them in the inspector. If you have 9 water images do

    public Texture2D[] waterImages; and assign them in the inspector.

    Then below //Show Frame in my script

    this.renderer.material.SetTexture("_MainTex",waterImages[frame]);

    Good luck :)
     
  8. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Thx Aaro I will see if I can get this to work appreciate the guidance.
     
  9. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    Debug.Log is a very heavy operation, try removing it from your loop and it just might work.

    Instead of using Debug log inside a loop, try this:

    Code (CSharp):
    1. string PosLog = "";
    2.         for (int i = 0; i < Nodes.Length; i++)
    3.         {
    4.             PosLog += " Node_ " + i + ": " + Nodes[i].Position + "\n";
    5.         }
    6.  
    7.         Debug.Log(PosLog);
     
  10. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    It's an infinite loop on the main thread. No matter what it will never work in his original way.
     
  11. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    I took the advice above and put it in the update so it runs now just lost on how to get it to render on the cube i made. Basically i imported 10 frames named 1000.bmp to 1009.bmo then assigned the values to frame so that frame would double as both the loop number int frame and frame.bmp name. Then the idea was to render these on a cube but I can't understand how to do that so i'm stumped.
     
  12. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906