Check out a free preview of the full Game Development with Unity for Web Developers course

The "Collectables Q&A" Lesson is part of the full, Game Development with Unity for Web Developers course featured in this preview video. Here's what you'd learn in this lesson:

Nick answers students' questions regarding what happens if the collectible does not have an implemented score method and if there is a formatter or linter for Unity code. How transform is being accessed in the Collectible file and if GameObjects are always a class of MonoBehaviour are also covered in this segment.

Preview
Close

Transcript from the "Collectables Q&A" Lesson

[00:00:00]
>> In terms of error handling, what if an item runs into collector doesn't have like a increment score method or the appropriate behavior?
>> It will cause an error. So let's actually go back to the script so I can show you how you might want to handle this.

[00:00:19]
So if I were to write an if statement and say if other.gameobject.get component score does not equal null. Then do that, however, in this game, there shouldn't be anything running into that, that doesn't have a score component. So that is a little bit of a simplification. So, actually, I wrote that out.

[00:00:51]
If the other game object has a score component, it's not actually doing this if check but theoretically, yes, it should. And if anything did run into it that didn't have a score component, it would cause an error. So, good question, yes.
>> Is there a formatter or a linter for Unity code?

[00:01:10]
>> I don't think so, I mean. I generally just get by with the tools that VS Code provides and what Unity provides as an install, but the no, I don't think so. I mean, I'm sure there's something like that out there. I've never bothered to look. Haven't needed it really.

[00:01:35]
>> Yeah, just doing so. Some quick searching it sounds like there's a C# format or built into the C# extension.
>> Okay.
>> This code so you can.
>> There you go.
>> Format on Save.
>> Cool, okay, any other questions?
>> This might be a silly question, but how are we getting access to transform inside of this file?

[00:01:59]
I don't see it declared or imported anywhere.
>> So it's part of the mono behavior class. So yeah, every game object has a transform. And so it can just be assumed that it's there and it will be able to grab it. So yeah, that's not something even though it is a component, it's not a component we need to call and get component on because every game object has to transform.

[00:02:35]
>> Our game objects class always requires a mono behavior?
>> No, so mono behavior is the component class. So that's how we create components that get attached to game objects. But mono behavior is able to find other components on its game object.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now