Check Y Position and If Statement

Hi,

I’m trying to check the Player Prefabs Position, if the y is less than -30 I want to initiate my Die script.
Here is the code that I have:

if (gameObject.transform.position.y < -30)
{
Die();
}

So this works if I call it in update, but it will keep spamming since its being called in update. What is the best place to call this? I tried making a private void but that also did not do it.
Since I’m a beginner I’ve been trying to learn how to use the C# API for unity by clicking the question marks in unity, but I don’t understand the usage of the commands, if you guys have any recommendations on how I can understand the unity documentation that would also be appreciated.

Checkin this each frame takes little to no time at all, so don’t stress about that.

I assume “spamming” refers to Die() being called every frame the GameObject is under -30, to prevent this you could add a bool which checks if the object is dead, alternatively disable the script when it’s called.

// Store "isDead" outside any methods so it belongs to the whole script.
bool isDead = false;

if (isDead == false && gameObject.transform.position.y < -30)
{
    isDead = true;
    Die();
}

// Alternative, turn off the script
if (gameObject.transform.position.y < -30)
{
    // Turns off this script component
   // Be aware that Unity API methods such as Update will not be called when the script is disabled.
    enabled = false;
    Die();
}
1 Like

A few minor corrections for how you call things. Your “Die()” is a method. You dont instantiate methods, you call methods. Prefabs are premade gameobject setups which you store in your project hierarchy. You can instantiate prefabs to turn them into actual gameobjects, which show up in the scene hierarchy ingame. You are not checking the y-Position of your player prefab. That would never change! You are (correctly) checking the position of your player gameobject.
Just to clarify what Prefabs are: Unity - Manual: Prefabs

As was already mentioned: small checks such as this wont affect your performance. You could have thousands like it, and would not notice. As a rule of thumb, especially as a beginner: do not worry about performance. This will slow your development to a crawl and take the fun out of it. If you ever stumble into an actual performance problem you can trace it to the correct culprit using the unity profiler, and then fix that specifically. Unless you know what you are doing, 99.5% of potential performance problems you think about and try to optimize wouldnt have ever become a problem in the first place, thus wasting your time. Performance is an advanced topic, but as modern hardware is orders of magnitudes stronger than it would have to be for most games, you dont really have to care too much :slight_smile:
Since i mentioned, here are the profiler links: Unity - Manual: Profiler overview
(But you dont need to look to deep into it for now, just know that it exists to help you)

As for understanding Unity, the easiest overview is this chart:

It tells you how everything Unity does is connected, and when what is executed. It may seem a bit overwhelming at first, but if you ever wonder, for example, when Awake() or Start() are actually called, or how the Update loop works, or when Collisions are actually checked, then this chart shows it all.

Other than that, if you ever wonder about the actual Unity API, check the documentation. The Unity documentation is one of Unitys strongest advantages. It’s really, really well done.

For example, you can search for things like “Vector3”, “Transform” or “Collision” and get a full overview of all involved properties, functions, and oftentimes some programming examples and a list of connected topics. This gives you a full overview of how these things can be used, and what they are even capable of doing in the first place.

However, while the documentation is amazing, it is more for specific topics. It wont teach you how to program or what to do in Unity, or the broader picture in general. If you are still very much at the beginning of your journey, i recommend the C# + Unity programming tutorial series by Sebastian Lague on youtube. He starts at absolutely 0 knowledge and builds form there, with what i find to be very intuitive mental models.

5 Likes

Thank you, this fixed it.

Thanks for the explanation, this was great and detailed. I cannot believe you took this much time into writing this, but this does help a lot <3.

3 Likes

The thoughts on performance here are spot on. That said, I believe it’s also important to understand the underlying logic and not do much (if any) unnecessary checks or calculations, even if the performance impact is negligible. In your case, once Die() is called, it shouldn’t need to be checked again (hence the bool). Use that sort of structure for anything that needs checking repeatedly—once a condition is met, stop checking.

Don’t rush into it as a beginner, but “event based programming” is the next step along these lines. In many cases it means you don’t need to check for stuff at all - your object gets told when it happens, and just does what it has to do.

2 Likes