Hey, @TheAwesomeJames!
No worries! I understand that for sure – most days I don’t get much free time, either!
If you put that method in your brick class (I haven’t actually gotten to this part of the course yet, so I’m assuming we have a script on the bricks that handles collisions. If not, I’ll hopefully get there tonight and can update this), then it would look something like this:
void OnCollisionEnter2D(Collision2D co) {
//whatever other logic here
Log("We hit the brick!");
}
//
//...other code in the class, maybe, placement doesn't matter.
//
public static void Log(string message, Object context = null) {
#if UNITY_EDITOR
Debug.Log(message, context);
#else
Application.ExternalCall("console.log", message);
#endif
}
Now, this isn’t great coding practice, because Log can now be accessed from anywhere in your code, but there’s no reason it should be on a brick, particularly. It would be better to put this on a GameManager or even a dedicated “Utilities” class. Because it’s public and static, though, you can call it from anywhere by using
ContainingClass.Log(yourString);
Compare this to if it were not static (but still public), in which case you need:
ContainingClass instance = new ContainingClass();
instance.Log(yourString);
That’s the crux of the “static” keyword – it means you don’t need to create an instance of a class in order to use the method.
And don’t worry, it’s not a silly question! As far as I’m aware, we haven’t covered calling methods from other methods in much detail. OnCollisionEnter will “pass control” to Log (which means log is now what the CPU is running). Log will execute (print its statement to either the Unity console or the Javascript console.log if we’re in browser), then it will pass control back to OnCollisionEnter, which at that point would just finish and your game would continue to run. The reason we can call Log from OnCollision enter is a little more complex. Let’s analyze:
void OnCollisionEnter2D(Collision2D co)
public static void Log(string message, Object context = null)
Since OnCollisionEnter2D doesn’t specify a protection level, it’s assumed to be private, which means nothing outside the class can reference it (you can’t call the object’s OnCollisionEnter2D from another type of object). However, since it’s within a class that can access Log (any class), we can still call Log from within it. Public doesn’t actually matter in this case, since it’s in the same class as OnCollisionEnter2D. But it would matter if we decided to put Log in GameManager or something similar, as mentioned above. Then, Log would need to be public so we could see it from another class (Brick, in this case).
Now, if that all seems a bit crazy, don’t worry – it is. We’re getting into the nitty-gritty details of Object-Oriented Programming with that. All you should need to remember is this:
Since it’s static, I can literally just call it from within another method (in any class that can access it), which will allow it to do what it’s supposed to, then we’ll return to the original method and continue as usual.
It’s important to emphasize that you can certainly call non-static methods (though not from within static methods), you just need to create an instance first.
Finally, don’t lose that confidence! This isn’t easy stuff, and it’s awesome that you got this far so quickly! It can be easy to lose determination when you encounter errors that aren’t your fault, but keep chugging! When you solve them, it’ll feel all the better!
Sorry this answer got super long, but I hope it helps! If you haven’t already, do try building for windows and seeing if that exhibits the same problems.