#```cs

1 messages · Page 1 of 1 (latest)

cloud oyster
#
    private GameObject[] bullets;
    private GameObject shootbullet;

    int FireRate = 0;

    void Update()
    {       
        if (FireRate == 60)
        {
            StartCoroutine(SpawnBullets());
            FireRate = 0;
        }
        else
        {
            FireRate++;
        }   
    }

    IEnumerator SpawnBullets()
    {
            yield return new WaitForSecondsRealtime(0);
            //makes custom keyword.
            shootbullet = Instantiate(bullets[0]);

            //actually sets the stats
            shootbullet.transform.position = this.transform.position;
            shootbullet.transform.rotation = this.transform.rotation;
    }```
#

aw how come your code has the pretty colors?

#

Anyways, A question I had about your psudocode, was that you put the start functionin an IEnumerator?

crystal cobalt
#

Yes, that is valid.

#

Start can be a coroutine and would automatically start.

cloud oyster
#

both, at the same time?

#

sorry, I don't mean simultaneous. I mean, like, Making it a Coroutine won't stop it from activating at it's normal time?

crystal cobalt
cloud oyster
#

gah friggin. I just realized I forgot to tie my code to the inputs

crystal cobalt
#

Ideally, you wouldn't want to be repeatedly starting coroutines but instead have a single coroutine for the life of the event and do what you need to in a loop.

cloud oyster
#

ah, okay. this is making sense

#

ooooh

crystal cobalt
#

So in your case, you'd: cs Update: Get input and save it to some field SpawnBullet: loop forever till this component is disabled etc loop while field that has input isnt valid yield to not freeze Spawn bullet since proper input was pressed yield delay OnEnabled: StartCoroutine(SpawnBullet) OnDisabled: //Don't need to do anything as a Coroutine is disabled when the MonoBehaviour is disabled

cloud oyster
#

wait, that's why the waitforseconds wasn't fixing the fire rate before. it was, it was just also creating about five million coroutines all waiting for seconds.

#

so eventually, it'd just look like a steady stream

#

Okay I think I'm getting this down, I've just got one minor issue

#

ffs nvm

crystal cobalt
# cloud oyster

Return type needs to be IE... else it's a regular function etc

#

Yeah.

cloud oyster
#

I left the yield return in there

#

yeah

#

I'm not asking for a bool tho

#

ALL of those red lines have the same error

crystal cobalt
# cloud oyster ???

First off, Update.
Second, you've got some field called Input that's of type bool.

#

Third, hopefully "LookVertical" and "LookHorizontal" are valid.

cloud oyster
#

yeah the speed at which everyone answered that makes me think this is a common mistake ^^

cloud oyster
#
public class Shooting : MonoBehaviour
{
    [SerializeField]
    private GameObject[] bullets;
    private GameObject shootbullet;
    private bool HasInput;
    [SerializeField] 
    float FireRate = 0.05f;


    IEnumerator Start()
    {
        while (true)
        {
            yield return new WaitForSecondsRealtime(FireRate);
            while (HasInput == false)
            {
                yield return null;
            }
            SpawnBullets();
        }
    }

    void Update()
    {
        if (Input.GetAxisRaw("LookVertical") != 0 || Input.GetAxisRaw("LookHorizontal") != 0)
        {
            HasInput = true;
        }
        else
        {
            HasInput = false;
        }
    }

    void SpawnBullets()
    {
        //makes custom keyword.
        shootbullet = Instantiate(bullets[0]);

        //actually sets the stats
        shootbullet.transform.position = this.transform.position;
        shootbullet.transform.rotation = this.transform.rotation;
    }
}```
#

This should look alright?

#

It's working how I intend for it to, at least

crystal cobalt
crystal cobalt
cloud oyster
#

thank you very much for teaching me the smarter ways of the programmer.

cloud oyster
#

sneaky developer sneakrets

#

alright, now tell me how to do fun colors in my codeblock text like you did at the top

crystal cobalt
#

I mean... cs if (the stuff in here are true) something = true else something = falsemight as well be... cs something = the stuff in here

#

!code

hidden violetBOT
#
Posting code

📃 Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/

📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.

crystal cobalt
#

Inline Code

cloud oyster
#
public class Shooting : MonoBehaviour
{
    [SerializeField]
    private GameObject[] bullets;
    private GameObject shootbullet;
    private bool HasInput;
    float FireRate = 0.05f;


    IEnumerator Start()
    {
        while (true)
        {
            yield return new WaitForSecondsRealtime(FireRate);
            while (HasInput == false)
            {
                yield return null;
            }
            SpawnBullets();
        }
    }

    void Update()
    {
        HasInput = Input.GetAxisRaw("LookVertical") != 0 || Input.GetAxisRaw("LookHorizontal") != 0;
    }

    void SpawnBullets()
    {
        //makes custom keyword.
        shootbullet = Instantiate(bullets[0]);

        //actually sets the stats
        shootbullet.transform.position = this.transform.position;
        shootbullet.transform.rotation = this.transform.rotation;
    }```
#

oh you beautiful fucker

#

okay thank you so so much wise teacher for your wisdom and free time

crystal cobalt
#

Looks good-to-go UnityChanThumbsUp