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): using UnityEngine; using System.Collections; public class gifanim : MonoBehaviour { // Use this for initialization void Start () { int frames = 1010;//named the images 1000-1009 so i could increment them easily for (int next=1000; next<frames; Debug.Log (next), next=next+1) if (next >= 1009)//last frame next = 1000;//want to keep it animating so return to start of clips. } } 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) 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
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.
Code (CSharp): int frame = 0; void Update(){ frame += 1; //Show frame //If we just showed frame 10. Go back to first if (frame == 10){ frame = 0; } }
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? 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 .
Vector3.distance Code (CSharp): void Update(){ Vector3.Distance(otherObject.transform.position,playerObject.transform.position); if(distance < 5){ frame += 1; //Show frame //If we just showed frame 10. Go back to first if (frame == 10){ frame = 0; } } }
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?
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
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): string PosLog = ""; for (int i = 0; i < Nodes.Length; i++) { PosLog += " Node_ " + i + ": " + Nodes[i].Position + "\n"; } Debug.Log(PosLog);
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.
Take a look at the accepted answer by Eric5h5 here: http://answers.unity3d.com/questions/59673/cycle-textures-over-time.html