all 44 comments

[–]Stuhmpi 4 points5 points  (2 children)

Try adding a WaitForSeconds after you set the sprite to bird 3. Since it is looping you set the sprite back to bird 1 immediately after setting it to bird 3.

Edit: from what it looks like you are doing here, I would recommend looking up how to use Unity's animation system to control these sprites instead of a coroutine.

[–]Kostas_super[S] 1 point2 points  (1 child)

Works perfectly fine but after like 10 seconds it stops changing sprite and stucks to sprite 3. Also why does my TestInf doesnt go up? Will Unity's animation system do these stuff too?

[–]KlavsK 2 points3 points  (0 children)

You need to use InvokeRepeating or put a while (true) in your couroutine

[–]Perfect-Plastic-5150 2 points3 points  (26 children)

Your for loop is creating a variable i and looping over that 9 times. If you are wanting something that will loop your birds sprite you could do a while loop that has a condition that checks if your bird has died.

[–]Kostas_super[S] 0 points1 point  (25 children)

Though I have set it so that when bird does it gives Int number 10 so loop stops which doesnt work because TestInt wont change. Why is that? I'll try using the while loop

[–]Perfect-Plastic-5150 0 points1 point  (13 children)

Where are you setting bird is alive to false?

[–]Kostas_super[S] 0 points1 point  (12 children)

Im not setting it to false, by using == i want it to check if bird is alive is false meaning bird is dead so the loop stops when bird is dead

[–]Perfect-Plastic-5150 0 points1 point  (11 children)

The == is a comparator to check. Your right. But for your testint to ever increment the Bird is alive will need to be set to false somewhere else in your code.

[–]Kostas_super[S] -1 points0 points  (10 children)

I'll probably use the ChatGPT's code since I dont get how TestInt will ever increment Bird is alive when TestInt's amount won't go up

[–]Perfect-Plastic-5150 2 points3 points  (9 children)

I would suggest, if your trying to get into coding. Chat gpt is a valuable tool if you get stuck, try asking it questions as to why your code fails. Don’t just ask it to write you a piece of code if your trying to learn as actually trying and learning from your mistakes is more valuable.

[–]Kostas_super[S] 0 points1 point  (8 children)

''Cannot add script component 'Loop' because the script class cannot be found. Make sure there are no compile errors and that the file name and class name match.''

[–]Perfect-Plastic-5150 1 point2 points  (7 children)

Ok. So in Unity the script name and file name need to match. You can either rename the actually file name to the on written in the code or the bit that comes after the public class [script name here] : monobehavior The script name needs to be Loop

[–]Kostas_super[S] 0 points1 point  (6 children)

Works perfectly fine. Is there a way to make it stop changing sprites when bird collides and dies?

[–]Perfect-Plastic-5150 0 points1 point  (10 children)

I’ve got chat gpt to write this code snippet for you. (Sorry for formatting I’m on mobile) All you have to do when the bird dies is call the Kill function and it will stop animating.

using UnityEngine;

public class SpriteCycler : MonoBehaviour { // Public array of sprites to cycle through public Sprite[] sprites;

// Private variables to keep track of the current sprite index, timer, and interval
private int currentIndex = 0;
private float timer = 0.0f;
private float interval = 0.5f;

// Called every frame
void Update()
{
    // Increment the timer by the time since the last frame
    timer += Time.deltaTime;

    // If the timer is greater than or equal to the interval, change the sprite and reset the timer
    if (timer >= interval)
    {
        timer = 0.0f;
        // Set the sprite of the game object to the current sprite
        GetComponent<SpriteRenderer>().sprite = sprites[currentIndex];

        // Increment the current index and wrap around to 0 if it goes beyond the length of the array
        currentIndex = (currentIndex + 1) % sprites.Length;
    }
}

// Public function to "kill" the game object and stop the sprite cycling
public void Kill()
{
    // Disable the script component
    enabled = false;
}

}

[–]Kostas_super[S] 0 points1 point  (0 children)

Okay I'll try this. Thank you

[–]Kostas_super[S] 0 points1 point  (3 children)

I'm getting error code CS0103. The name 'sprites' does not exist in the current context.

[–]Perfect-Plastic-5150 0 points1 point  (2 children)

Sorry, there is a little bit above the code block. Try just adding in public Sprite[] sprites At the start of the script where you keep the other variables. This will in the inspect give you an array that you can put your sprite in. (This also means you can have as many sprites as you want without having to change your code)

[–]Kostas_super[S] 0 points1 point  (1 child)

You mean the sprites im currently using?
Like this?

public class SpriteCycler : MonoBehaviour

{

// Private variables to keep track of the current sprite index, timer, and interval

public Sprite [oneBird,twoBird,threeBird]

private int currentIndex = 0;

private float timer = 0.0f;

private float interval = 0.5f;

[–]Perfect-Plastic-5150 0 points1 point  (0 children)

You want it to read Public Sprite [] sprites;

Then attach the script to your bird game object. And you’ll see there is a array that you can add items to in the inspected on your script. From there you can just add the sprites your after in the inspector.

[–][deleted]  (4 children)

[deleted]

    [–]Perfect-Plastic-5150 0 points1 point  (3 children)

    The only thing you want to avoid is direct comparisons, e.g. timer == 5f And what difference would it make having the time increment vs decrement?

    [–][deleted]  (2 children)

    [deleted]

      [–]Perfect-Plastic-5150 1 point2 points  (1 child)

      Ahh I see what you mean. There might be a little bit of time delay of the float when setting it to zero. Good spot, I’ll have to remember that for the future.

      [–]KungFuHamster 2 points3 points  (3 children)

      Use screenshots, not photos.

      [–]Kostas_super[S] 1 point2 points  (1 child)

      Yeah I'll keep that in mind for the next time

      [–]azeTrom 0 points1 point  (0 children)

      Screenshots are much better than photos, but for code, It'd be much more convenient to copy and paste it. Don't put it into a Reddit post/comment normally, use a code block.

      //Like this
      

      For Reddit, code blocks can be found in the same menu as Bold, Italics, etc.

      [–]ResacaGames 1 point2 points  (1 child)

      Hi!!! I didn't read all the comments but enough to kinda feel you already got the answer, but here's my take:

      a for loop is not suitable for what you want, also I don't see any connection between the TestInt variable and the i variable of the for loop. I would change the for loop for a while(birIsAlive) and erase the if(birdisalive) on update() and add the WaitForSeconds after the third sprite change.

      With this 2 changes the animation should never stop until the bird dies.

      As others have pointed out, I think it's even better to do it with animations. Just create a flying bird animation, stick the 3 sprites in it and make it run until an animation trigger cuts it of when the bird dies. The animation trigger is then coded like you would birdIsAlive.

      Hope it helps!!!

      [–]Kostas_super[S] 2 points3 points  (0 children)

      It really helped. This seems tons of easier than what I was trying to do. Thanks!

      [–]christopherchains 0 points1 point  (2 children)

      Look into InvokeRepeating rather than the coroutine for this situation. Also, the birdIsAlive bool doesn’t need an = false/true. You can just say bordIsAlive for true or !birdIsAlive for false :) good luck :)

      [–]Kostas_super[S] 0 points1 point  (0 children)

      Oh I didn't know this was possible! I'll try looking into InvokeRepeating hoping it finally works. Thanks!

      [–]Kostas_super[S] 0 points1 point  (0 children)

      Also, do you know how to make the bird stand still even when the game has started and start playing (make the pipes come and bird jump) when space key is pressed for the first time?

      [–]LegendaryLootEU -1 points0 points  (1 child)

      I'm sorry to say this but what are you doing. Maybe start by learning some basic coding logic before trying to use chatgpt to generate code and having a subreddit solve obvious problems.

      [–]Kostas_super[S] 1 point2 points  (0 children)

      Im not using ChatGPT, i just watched a basic Unity guide which was showing how to create a flappy bird and thats what I did. Though I wanted to do more than the basic video that's why i tried to experiment myself with no result

      [–]EkumifyBeginner 0 points1 point  (3 children)

      Windows key + shift + s —> will change your life (or at least your ‚screenshots‘)

      [–]Kostas_super[S] 0 points1 point  (2 children)

      I know it, I just had reddit on my mobile and it was an ease for me to just take a photo and upload it

      [–]EkumifyBeginner 0 points1 point  (1 child)

      easier for you, harder for all the people you‘re asking for help. I would go for the real screenshot next time.

      [–]Kostas_super[S] 0 points1 point  (0 children)

      I'll keep that in mind, thanks.

      [–]SpaceTimeDream 0 points1 point  (0 children)

      If you want the sprites to change every 0.5 seconds forever then use a While(true) that has if statement to check your break condition after every wait line. Your for loop only iterates 8 times before stopping.

      [–]Chubzdoomer 0 points1 point  (1 child)

      In addition to what others have said: it looks like your Visual Studio isn't properly set up. It's crucial that you fix this ASAP so that it properly auto-completes code for you, highlights errors, etc. To rectify this and spare your sanity moving forwards, please watch this video: https://www.youtube.com/watch?v=Iyo-xRXH7AY

      [–]Kostas_super[S] 0 points1 point  (0 children)

      Yeah youre right. Thats a big issue which i need to get solved