OnCollision problems with fuel

Had to switch the Fuel component to “is trigger” so that the rocket would not bounce off it.
This required to add another branch into the collision script OnTriggerEnter(Collider other)

now it registers hitting the fuel, but does not bounce off it.
Then put in Destroy(other.gameObject) to destroy the fuel canister

3 Likes

Great job finding the solution for this!

Just a quick tip; Try using the CompareTag method instead, it’s faster than making a “manual comparison”.

1 Like

This isn’t very helpful if you don’t explain that CompareTag() returns a bool, while the switch statement demonstrated in the video is based on evaluating strings. If we’re going to go into the weeds of premature optimization, then we wouldn’t use a switch statement at all, as they’re not really considered a good practice. But since at this point we’re just explaining the concept of a switch statement at a beginner level, using the simplest implementation (direct string comparison) is fine. It’s not really worth the frustration of converting it to a CompareTag() implementation.

CompareTag() is not just about optimisation. It is safer and should always be preferred over a straight string comparison and since this is a beginner course, it is the best place to introduce it.

CompareTag checks that a tag exists and will throw an exception if it doesn’t. A string comparison will just see that it doesn’t match and go along its merry way. This can be hard to debug if you don’t realise that you made a typo in the tag or - like I did yesterday - intended to create a tag and forgot. Fortunately using CompareTag() came to the rescue and it was sorted immediately.

1 Like

It should also be preferred to never use switch statements in the first place, but since this entire lesson exists only to demonstrate what a switch is and how to implement it in the simplest terms, there’s absolutely no reason to overcomplicate it with a CompareTag() function.

And if someone wants to come in and say “use CompareTag() instead!” to an absolute beginner, they could at least have the courtesy to demonstrate the implementation of this concept, which hasn’t been broached at all in the course up to this point.

CompareTag checks that a tag exists and will throw an exception if it doesn’t.

Believe it or not, that is in fact an optimization.

I don’t see how this is overcomplicating things, it removes lines and it’s way easier to read.

I’ve been in these forums for almost 10 years, I’ve mentored a lot of students in and outside the forums, and almost everything I say (with the exception of my terrible dad jokes) is to try to improve the student’s learning experience. The reason why I suggested that is because using the equality operator to compare tags is something a lot of students tend to stick with. Here’s a video of Dr. Penny de Byl, one of the best, if not the best Unity instructors out there, saying that you should forget about comparing tags with the equality operator even when she has used them many, many, times before, proving that even experts overuse it, that’s why I suggest this early the CompareTag method, because even Dr. Penny de Byl, years after that video, still uses the equality operator in her new courses. Sometimes, it’s all about building a habit.

I didn’t mention the Switch statement because as the student progresses and is more capable of creating better coding architectures, Switch statements become a thing of the past, it’s a very natural process, unlike using the equality operator to compare tags.

You should probably better consider the context of the advice you’re giving, you might as well say “Forget this lesson entirely, because it’s bad coding practice.” I agree that you shouldn’t use switch statements, but how would a beginner know that? How would a beginner translate your advice of “Use CompareTag()” without a little bit of explanation that you’re also going to be eliminating the switch statement in order to do so, when the lecture at hand is explicitly about how to properly implement switch statements?

It’s the same kind of “just Google it, newb” nonsense that used to pollute the old Unity Answers site.

Pedagogically speaking, this lesson only exists to demonstrate switch statements and how they work. That’s it. You need to know what they are before you learn alternatives to them. Could Rick have chosen something other than string references to demonstrate this? Sure. But that’s not the point of this lesson.

So again, the least you can do is offer some explanation to the original poster or other absolute beginners as to the why of your recommendation, rather than just barking an optimization that has nothing to do with the lecture it’s posted against.

Oh… now I understand why you said this overcomplicates things.

Look, I’m not trying to be mean, but the one that needs to be in context is you.

I suggested using the CompareTag method instead because of the following block of code:

private void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "Fuel")
    {
        Debug.Log("Jet Fuel, keeps you going")
        Destroy(other.gameObject);
    }
}

That’s the block of code the student was talking about. This has nothing to do with refactoring the Switch statement. Read the entire post, check the student’s code, and then give feedback, don’t just assume things.

One last thing, I never said that Switch statements shouldn’t be used, that’s you, and you alone, I don’t like that “never do this” mentality when coding, yes, there are good practices and bad practices, but sometimes bad practices are the right way to go, it depends on the situation, for instance, in the studio I’m currently working, one of the Writers has no idea how to use Unity, so we created a JSON database that basically renders the real-time part of the engine obsolete, terrible idea, yes, but it helps our writer get things done without having to learn a new tool on a rush, the writer can take time thanks to that system. Never say never.

Privacy & Terms