#```using System Collections

1 messages · Page 1 of 1 (latest)

tardy pine
#

Let's start a thread to have things clean here

#

First, why Invoke and InvokeRepeating is bad is because there's a lot of overhead and it's costly

#

It's alright in the scale of the tutorial you're following, but in a bigger project it could be problematic

tight oracle
#

Is being cleaner related to optimizations?

#

I don't know if you have used unity learn before, but it's a challenge and they gave me a project full of errors and they asked me to make it as desired, so I'm trying to fix it.

tardy pine
#

Sometime, cleaner means your code is more readable so that people can easily fix or extend it

#

(and you can easily understand what you did after not touching your code for 6 months)

tight oracle
#

İ understand

#

Now

tardy pine
#

Now here is an example with a simple timer

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnManagerX : MonoBehaviour
{
    public GameObject[] ballPrefabs;

    private float spawnLimitXLeft = -22;
    private float spawnLimitXRight = 7;
    private float spawnPosY = 30;

    private float startDelay = 1.0f;
    private float spawnInterval = 4.0f;

    // we create a variable that will track the next time we spawn a ball
    private float nextSpawnTime;

    // Start is called before the first frame update
    void Start()
    {
        // Since you want a delay, the first spawn time needs to be initialized so that it takes it into consideration
        nextSpawnTime = Time.time + startDelay;
    }

    // Update is called every frame
    void Update()
    {
        // If the current time is past the time we are supposed to spawn something, we do stuff
        if(Time.time >= nextSpawnTime)
        {
            // spawn ball
            SpawnRandomBall();

            // The next time we want a ball to spawn is our current time + cooldown
            nextSpawnTime = Time.time + spawnInterval;
        }
    }

    // Spawn random ball at random x position at top of play area
    void SpawnRandomBall()
    {
        // Generate random ball index and random spawn position
        Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
        int ballIndex = Random.Range(0, ballPrefabs.Length);

        // instantiate ball at random spawn location
        Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
    }
}
#

I just modified your code and commented "important" stuff

tight oracle
#

Let me sec i will copy and try

#

= what does it mean

tardy pine
#

Greater or equal to

tight oracle
#
// We create a variable that will track the next time we spawn a ball
    private float nextSpawnTime;

    // Start is called before the first frame update
    void Start()
    {
        // Since you want a delay, the first spawn time needs to be initialized so that it takes it into consideration
        nextSpawnTime = Time.time + startDelay;
    } ```
#

Here

#

Why we dont

#

private float nextSpawnTime = Time.time + startDelay;

#

And just we can use in void Start ‘nextSpawnTime’

tardy pine
tight oracle
tardy pine
#

Did you try it ?

#

Just try it 🙂

tight oracle
#

İ am trying now 1 sec

tardy pine
#

You're supposed to get an error

tight oracle
tardy pine
#

Another reason I did it that way

#

Start is always called the 1st frame your spawn becomes active. So we make sure the delay is set at that time. Doing it in a field initializer, you wouldn't be sure of when the time your spawner would become active, then it could spawn a ball instantly instead of taking the delay into consideration

tight oracle
#

İ understand

#

but the ball spawn interval still spawns at 5 seconds not random between 3-5 seconds

#

Can i send short video?

tardy pine
#

I assumed it was a fixed interval

tight oracle
#

I need to do to progress in the course

tardy pine
#
private float minDelay = 3f;
private float maxDelay = 5f;

void Update()
    {
        // If the current time is past the time we are supposed to spawn something, we do stuff
        if(Time.time >= nextSpawnTime)
        {
            // spawn ball
            SpawnRandomBall();

            // we pick a random float delay in the range you defined with min and max
            float delay = Random.Range(minDelay, MaxDelay);
            // The next time we want a ball to spawn is our current time + cooldown
            nextSpawnTime = Time.time + delay;
        }
    }
#

you can make both minDelay and maxDelay visible in inspector by adding [SerializeField] before each of them. That way you can edit the values without recompiling

tight oracle
#

[SerializeField]?

#

Public isnt same ?

tardy pine
#

You could make it public. It's alright.
But for better practice and learning : you should try to avoid public field if no other class has to access it.

#

[SerializeField] tells Unity a field has to be serialized and displayed in the inspector. public fields are serialized by default so it doesn't require it, but private are not so we have to use this attribute

tight oracle
#

When we make it public, can we access that variable from other scripts?

tardy pine
#

Yep

tight oracle
#

Wow i didnt know this

tardy pine
#

And we want to limit as much as possible who can modify our variables. So if your class is the only one that needs to modify its data, make fields private

tight oracle
#

I thought you were making the task of serializeField public. I didn't even know SerializeField existed lol

tardy pine
#

There's more to know about how to make stuff available outside of your class, but you're not there yet so let's try to keep things simple with private and public 😛

tight oracle
tardy pine
tight oracle
#

Junior Programmer Pathway really 0 to job ready?

tardy pine
#

I didn't do it up to the end so i don't rly know

tight oracle
tardy pine
#

I just took a peak when they published it, but it didn't exist when i learned unity

tardy pine
tardy pine
tight oracle
#

İ just started a 2 month ago

tardy pine
#

Well, yeah, and i'm still learning

#

But it's alright

#

Being a developer means learning continuously, there's no point where you don't learn anymore

#

Just, try to follow the course, and when you're in doubt, ask questions just like you did there 🙂

#

This discord and unity forums are a nice resources to have

tight oracle
tardy pine
#
  • ask Google obviously. Sometimes people asked questions on other platforms and can be VERY helpful
tight oracle
#

I'm thinking of becoming a game developer. After the junior programmer, I will start the creative core course. Should I start making the first simple games after that too, or should I find other courses and continue learning? (İ dont have any team. İ am alone)

tardy pine
#

Last thing, if you don't know a something does, there's a chance you can have an explanation + example in the unity documentation https://docs.unity.com/

#

Just make sure you're on the same version as your editor

tardy pine
#

So that's a start

#

You don't need any team to build a game as long as you keep things in your reach

tight oracle
#

İ tried to use ready asset

#

Pink material issue and how i create a map/level design ?

#

They didnt learned that

tardy pine
#

Oh

tight oracle
#

İ designed a basic little game

#

İt progress a levels

#

But i dont have any idea

#

So i decided to waiting to finish both course

tardy pine
tight oracle
#

İ just create new project

#

And i import assets in assets store

tardy pine
#

OK

#

It's kinda hard to explain but the assets you imported are using features that are not integrated to unity by default

#

That's why they appear pink

tight oracle
#

Oh

tardy pine
#

It would be overwhelming to put you on the path to setting up URP, so just focus on unity path for now

#

They may come to URP later

tight oracle
#

Yes i think that too

#

Soo thanks you for all help

#

it was a pleasure talking to you

tardy pine
#

Np ! Good luck learning Unity !

tight oracle
#

Thanks again

tardy pine