theeory: This is going to be a quick one Zab, I promise.
Zab: Yeah, that’s what you said last time. Btw, I didn’t get paid yet for the last training session…
thee0ry: Everything is about money to you, isn’t it? Gah! What I’m offering you is more valuable then money. I offer you the ability to rotate and move objects with your code! Now, enough squandering time! Let’s drill down on the core here. The methods in discussion today will be:
Zab: Didn’t we just cover a topic like this!?! We were enabling and disabling in Lesson 11 with dot enabled!
thee0ry: Ah, an honest mistake if I’ve ever seen one, ol’ boy! You confuse dot enabled, which is really for enabling and disabling Components of Game Objects, with the different method that we are covering today:
thee0ry: ISN’T SHE A THING OF BEAUTY! This marvelous built in method from Unity will enable and disable entire GAME OBJECTS, not Game Components.
Zab: I see, and what’s the..
thee0ry: ..AND, there is a GUI for it as well, if you needed reference! Look down below, Zab!
Zab: You’re excitement almost seems disingenuous…but I’ll play along…for now. Anyway, that seems like the best short way to remember the difference! dot enabled disables/enables Game Components, and dot SetActive() disables/enables entire Game Objects! Cool. Now, mister know-it-all, can you give me an example of a case we would use this in Game Development?
thee0ry: If I may, imagine you were making skating game. Somewhere, there is a classic, heaven-forged piece of sushi floating and spinning as an icon, similar to the VHS icons in the timeless game of Tony Hawk Pro…
Zab: Stop, you’re losing the audience! It’s too cliche! thee0ry: How is this cliche, you fool!? It’s a classic! Zab:While that may be the case, your still just being another millennial on the internet who has a blog and occasionally browses Reddit who also happens to think Tony Hawk 2 is the butter to any toast! Drop it! Pick Mario, and move on!
thee0ry: I could’ve done without the image. We’ll discuss this later… thee0ry: Wait a minute… they can see our conversation! So….this is what XXXXXXXX™ Company Policy 12.A meant when it said “You may occasionally realize there is indeed a 4th wall to be broken, only for it to be forgotten or regressed soon after — for your own fictional sanity. Fun Fact: Most life thrive somewhere in the middle of being self-aware/willingly-oblivious! It’s okay, buddy! We all do it! It’s the XXXXXXXX™ company way!”….
thee0ry: If I may, let us use Super Mario as an example. Imagine you were working on Super Mario 64, and Nintendo assigned you with making sure that a Game Object, in this case a box crate, gets removed from the game when Mario performs the glorious butt-stomp. We don’t want to destroy it, we simply want it set to SetActive(false) so that it conveys a sense of strength! Marios Butt-Stomp is not to be trifled with, and that box has no business existing if a Butt-Stomp collides with it! In the same breathe this lets us access the object at a later time because we are not destroying it.
Zab: I see! So since we have the syntax already, I’ll cook up a little script! Let me know what you think:
public class ActivateObjects : MonoBehaviour
protected bool MarioButtStomp; // declared a protected variable for a boolean about booty-romping Butt-Stomp
public GameObject MarioCrate; // declared a public variable the MarioCrate GameObject
if (MarioButtStomp == true)// if the coveted Butt-Stomp is true
MarioCrate.SetActive (false); // then disable the feeble box
MarioButtStomp = false; //Set the butt-stomp off. Holster that attack!
Please note: This code is super loosely written. Might as well be pseudo code, at this point! I just figured it would get the idea across! I didn’t want to muddle the simple idea with Triggers, Collision, and code of the like.
thee0ry: Exactly, good sir! You’re not as slow a learner as the Recruiter made you sound! Perhaps you’ll do well in this company after all! Please, don’t ask me what the Recruiter thought of you, either. In fact, don’t ask funny company related questions — period. But stay curious! 😀
thee0ry: Right. Now, when we are dealing with Objects, we know that there are cases in which we have Parent Game Objects and Child game Objects, right? Do you know what we are referring to?
Zab: Well, I don’t believe we’ve covered that specifically, but I’ve heard it loosely mentioned here and there. To my understanding it is a hierarchy system, similar to many programming languages, OS behaviors, and even the Unity Editor!
thee0ry: What’d you draw that on, MS Paint? Crude. But the idea is there. When you are enabling/disabling the SetActive() of a GameObject, you must be cognizant of the Parent / Child Object relationship. Here are some golden rules to remember:
If you SetActive(false) a Parent Object, any Child Objects also go with it.
If you SetActive(true) a Child Object while the Parent Object is SetActive(false), then you will not see it active on the scene. In order for Child Objects to be active in the scene, Parent Objects MUST be SetActive(true).
thee0ry: With this in mind, you may find it useful to check if your Objects are active in the scene through Debug scripts.
Zab: How so? Won’t I just know by looking at the screen?
thee0ry: Imagine if you were making a game the size of Skyrim… would you still be able to check everything in the screen? I’m sure you can imagine, things get messy. Scripts are essential!
Zab: Got it. 0_0
thee0ry: The two methods to concern yourself with when checking for an Objects Active status in code is:
GameObject.activeSelf: The local active state of this GameObject. (Read Only) This returns the local active state of this GameObject, which is set using GameObject.SetActive. Note that a GameObject may be inactive because a parent is not active, even if this returns true. (ring any bells?)
GameObject.activeInHierarchy: Is the GameObject active in the scene? This lets you know if a gameObject is active in the game. That is the case if its GameObject.activeSelf property is enabled, as well as that of all it’s parents.
Zab: I see. So .activeSelf lets you know if the game object is active, regardless of it’s parent being active or inactive. In other words, if you check the status of a child object and its set to active, it will return true, even if the parent object is disabled — thus leaving this object inactive in the scene!
thee0ry: Correct, correct…
Zab: This leaves us with .activeInHierarchy, which checks to see if the object is ACTUALLY in the scene. This means it will return true (object is active) ONLY if the child object and all of it’s parent objects are active as well?…
thee0ry: Spot on! Nailed it! Right on the nose! Keep in mind, this info is most likely going to be printed on a Debug Logger. print() or Debug.Log will do just fine. That’s all the time we have for today, hope to see you in next lesson Zab!
Undeniably,there will be points in time where you have to enable/disable an object within a video game.
A perfect example would be a light switch. You want to enable a light object if the switch is pressed to On and disable a light object once a switch is pressed to off. This light object is in your Editors hierarchy, thus making it a part of the Scene once the Play button is pressed.
In this Scene, we have created a script and attached it to the light object as follows:
I write this to you with no idea how to deliver this information. All I know is that I must deliver this information. Something tells me I must. Many of it will be a re-iteration of already explained information. But…what isn’t? This is more for me then it is for you, potential reader. This is for me to tell myself “You’re doing good, kid”. I WILL NEVER STOP UNTIL I MAKE THE LIKES OF MIYAMOTO PROUD!!!!
By now you may have noticed a lot of talk in the Unity docs regarding Vectors. But what are they exactly? Is it an objects position? Does it have anything to do with images? HELP!
What is a Vector?
– Put simply: a Vector is a line drawn between two points. Not exactly the answer you were expecting, huh? Well, tough! A lot of your answers won’t be filled with magic and rainbows!
– You can’t speak about Vectors without mentioning magnitude: the length of a Vector.
That was cute…but seriously, what is a Vector?
I’m not pulling your chain. On the very base, this is what a Vector and magnitude are. Luckily for you, in relation to what you need to know for Game Development and Unity, it gets more complex:
– A 2D-Vector is a way of representing a point from the Origin point (0,0) to any point on the 2D plane. Also note: Since it is relative to the Origin point, it has an implied direction.