#```using System Collections
1 messages · Page 1 of 1 (latest)
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
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.
not always. but in this case yes.
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)
Ahh if is a game we need that for updates
İ understand
Now
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
Greater or equal to
// 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’
inside or outside the start ?
Outside
İ am trying now 1 sec
You're supposed to get an error
Yea its not working
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
İ understand
but the ball spawn interval still spawns at 5 seconds not random between 3-5 seconds
Can i send short video?
Oh you want it to be random, then let's update it
I assumed it was a fixed interval
Not exactly i wanted : (
I need to do to progress in the course
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
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
When we make it public, can we access that variable from other scripts?
Yep
Wow i didnt know this
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
I thought you were making the task of serializeField public. I didn't even know SerializeField existed lol
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 😛
I will pay attention. thank you
Yea🥲
Well, that's the weird part with Unity Learn. They wants to be beginner friendly, but they kinda give bad habits that goes in opposite direction with their coding guidelines
Junior Programmer Pathway really 0 to job ready?
I didn't do it up to the end so i don't rly know
Before the using unity learn, i tried youtube but they arent have good instruction
I just took a peak when they published it, but it didn't exist when i learned unity
Yeah, it's kind of a mess sometimes
How do you learned?
Well, a mix of videos and trials and errors. + some code review by classmates, colleagues, etc
And it took a lots of year
İ just started a 2 month ago
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
İ dont have any idea where is the unity forums
- ask Google obviously. Sometimes people asked questions on other platforms and can be VERY helpful
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)
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
Alrightt
If i remember correctly, the path makes you build a simple game for the duration of the course
So that's a start
You don't need any team to build a game as long as you keep things in your reach
Yea they said start to create your game but
İ tried to use ready asset
Pink material issue and how i create a map/level design ?
They didnt learned that
Oh
İ designed a basic little game
İt progress a levels
But i dont have any idea
So i decided to waiting to finish both course
Pink material is a rendering error. Did you import something like URP or used a predefined project to create your own?
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
Oh
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
Yes i think that too
Soo thanks you for all help
it was a pleasure talking to you
Np ! Good luck learning Unity !
Thanks again
Same here ! Have a nice day !