Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[INVALID/MOOT] This example code is not syntactically correct in any way.

Discussion in 'Documentation' started by asperatology, Feb 28, 2016.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Here's the article in question.

    And here's the example code shown in the article:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     void ApplyDamage(float damage) {
    6.         print(damage);
    7.     }
    8.     void Example() {
    9.         gameObject.SendMessage("ApplyDamage", 5.0F);
    10.     }
    11. }
    It is completely incorrect. There's no "gameObject" variable to be found, nor a "print" method. It may seem like an obvious case where the user knows what to do with the codes given, but it would make the point across more directly if the example code is all syntactically correct.

    A simple

    Code (csharp):
    1. public void print(float value) {
    2.          Debug.Log("Damage dealt: " + value);
    3. }
    Would suffice. The "gameObject" could be worked into the ExampleClass.
     
  2. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    gameObject is a member of MonoBehaviour. As far as print(), I think you are right.
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    People would say it may be referring to an external variable due to the confusion caused by print() (I am one of those people), so to clarify this a bit more, I would propose the "gameObject" be renamed to:

    Code (csharp):
    1. this.gameObject.SendMessage("ApplyDamage", 5.0F);
     
  4. feiting

    feiting

    Joined:
    Oct 26, 2012
    Posts:
    33
    This is mostly C# usage preferences. gameObject can be safely assumed, assuming C# is known before reading this. As far as print() assumption, eh, 50/50. It's one of like, 2 things that are "safe" to assume, in my opinion. Although, also in my opinion, Unity's target audience isn't programmers, so they should probably be explicit to account for newness
     
    aer0ace likes this.
  5. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Exactly.
     
  6. feiting

    feiting

    Joined:
    Oct 26, 2012
    Posts:
    33
    Hmm. but if they do that, I won't get my neatly organized docs... I must vote the other way
     
  7. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I advocate the explicit "this" for the sole reason seeing a "gameObject" and "print()" being together, otherwise people who aren't used to how MonoBehaviour is constructed will not know about "gameObject" being a class member. I had that fair share of frustrating confusion when I was transitioning from Unreal to Unity, and I was looking at "gameObject" as if it's some vague variable that you don't specify where it is obtained.

    I would advocate the other way, only when "print()" is written, as well as keeping the example code syntactically correct, and point out the fact "gameObject" is a class member.
     
  8. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358
    I disagree with using "this." in front of gameObject. It's a fundamental part of C# that properties can be inherited, and if we add "this." in front of every mention of gameObject, it will have the effect that people think they *have* to write that. I don't see why writing "this" will make it any clearer to non-programmers, because it doesn't actually explain anything - it just adds an extra word and symbol that they will then think they have to type.

    print() is a method that is also inherited from MonoBehaviour class, so it works as written. So contrary to the inflammatory subject line, the example you linked to is in fact syntactically correct in every way!

    However, I think there is a problem with the c# example - not in syntax or compilation - but in that it doesn't actually do anything if you drop it on a gameobject and press play. The reason for this is that it doesn't use a Start() or Update() function, instead the function is called Example() which won't be automatically called.

    If you rename the Example() function to Start(), place the script on a gameobject, and press play, the value of 5 will be printed to the console as expected.
     
    DanielQuick likes this.
  9. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Retracting my entire statement and thread, because I did not know print() is actually a MonoBehaviour method prior to your post. I was taught from the very beginning to use Debug.Log() instead of print(), and print() was deprecated... Apparently, it's currently not deprecated at all.

    Since print() is there, there's even no need for "this.gameObject" in the first place, henceforth, this whole point is moot.

    Sorry.
     
  10. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358
    Hehe no problem :) And don't be put off reporting problems in the documentation in future, we always appreciate it. But it's probably best to try the code itself before being sure there's a problem with it ;)