Search Unity

Solve Your Scripting Issues; checklist/guide 2

Discussion in 'Scripting' started by Codetastic, Sep 14, 2012.

  1. Codetastic

    Codetastic

    Joined:
    Aug 13, 2012
    Posts:
    23
    Preface
    This thread serves as a check list to common problems to look for when you run into a problem well programming. This is NOT a tutorial for learning scripting in Unity. If you are looking for tutorials for Unity please go to the Teaching section. This thread is a redone and upgraded version of my last smaller and less detailed checklist.



    Step 1: Check it
    When tiring to solve a problem with a script/s; the first thing you should do is make sure you do not have anything wrong with your syntax and/or spelling. This can be many simple mistakes such as:

    • Missing or extra brackets, parentheses or quotation marks (hate this one):
      Code (csharp):
      1.  
      2. function Foo(){{
      3. }
      4.  
      5. function Foo){
      6. }
      7.  
      8. function Foo(){
      9.  
      10.  
      11.  
      12. function Foo(){
      13.      print("foo);
      14. }
      15.  
      It should also be noted that strings with single or double quotation marks inside them can cause trouble. If you find yourself with a string that has quotes inside it you have to "escape the quotes" (like how Eric5h5 describes "here"):
      Code (csharp):
      1.  
      2. //notice it changes based on what you surround strings with
      3. //if you surrounded your strings with double quote marks
      4. print("It's a \"monster\"");
      5. //or single
      6. print('It\'s a "monster"');
      7.  
    • Misspelled words. transform works, transfrom does not.
    • Incorrect case letters. Their is a big difference between GameObject and gameObject.
    • Lack of a "end of line/end of statement/end of command". In many cases with Unity this is a semicolon " ; " at the end of a line of code:
      Code (csharp):
      1. print("foo");
      However boo does not use a semicolon as a "end of line", but rather follows a syntax more like "reading a book".
      To learn more about boo you can go "here".
    • Function not being called or conditions never met. In some cases your section of code is not even being called. To check this use the "Debug.Log();" or "print()" (identical to Debug.Log but shorter) to check if your code block is actually being called. Multiple Debug.Log's can also be used to check what order you code is running in.
    • And of course the dreaded comma instead of a period. Known to cause utter chaos.



    Step 2: Check it... again
    Is your script good but still not working? Perhaps it is not a problem with your script after all!
    But rather a issue in the Unity editor? For example:

    • You did remember to actually set the inspector values... right?
    • Is the script actually attached to the object? So simple right? However failure to check can lead to many hours or even days of headbanging... not that I would know...
    • OnCollision/OnTrigger functions not being called? Make sure to check that isTrigger is set correctly. And remember a rigidbody must be attached to one of the gameObject's for the function to even be called in the first place.
    • Debug.Logs, prints, errors and warnings only displaying once? Toggle "Collapse" in the console to off (shown as on in the photo):
      $Console-0.jpg



    Step 3: Search it
    Still not working? Well then it is time to look for help online, BUT WAIT! This does not mean go directly to the scripting forum and writing out a thread. Before you even think of doing that use the mighty powers of Google! 90% of the time your question has already bin asked and answered, on ether the forums or somewhere else... many times sometimes...

    Oh and don't stop with a single search, try multiple forms of wording of your question. Tip, if your looking for something unity specific. Searching with "unity3d" or "unity" before your question can help filter out unrelated information.



    Step 4: Sleep on it
    By this point you are probably going insane. And at that, just stop working on it. Creativity seems to flow more slowly when frustrated. So work on something else or take a break.



    Step 5: Start it over
    Sometimes their is no way to fix your script because it's logic in itself if flawed. If you ever find yourself in such a circumstance, then it might be time you tried a new approach.



    Step 6: Ask it
    Alright are you sure nobody has asked your question yet? Then ask. You will find many people on the forums/unityanswers are willing to help. And as long as you actually tried to find the answer yourself, most will be happy to do so. But in order for someone to help you, the must find and be able to understand you question. Some tips for posting questions:
    • Use a descriptive title to your thread. Titles like "HELP ME" or "NEED SCRIPT FIX NOW!!!!" will not help you. In fact this can act as a deterrent to other programmers. After all what thread would you open, the proper and descriptive question; "How to normalize a Vector3" or the noob yelling; "NEED HELP SCRIPT NOT WORKING" at you to do all the work?
    • Try to use correct grammar and spelling in your thread. If those are not your best subjects or English is not your first language (and even if it is). Don't worry about it, write it your best and be descriptive, it will help to improve the odds of someone understanding your question.
    • If possible post visual content of your problem; images, screenshots, web players and videos of what is going on might help someone identify what the problem is.
    • Do not! Post your question in multiple sections of the forum. This is frowned upon by many users. Instead find the one forum that best describes your question and post there. If you are unsure it is in the right place, or posted in the wrong section, do not worry to much about it. Simply apologize for not knowing where it should go. A moderator might even move it to the right place for you if you ask one nicely.
    • Do not over bump your thread. It does not have to be at the top to be answered. A normal accepted limit of bumping would be once every 1 or 2 days.
    • When posting your code, be sure to use the"code" tags. This will make it much easier for other people to read your script. For example of code tags click the link. A example of code tags in use:
      Code (csharp):
      1.  
      2. var foo : int = 0;
      3. function Start(){
      4.      Debug.Log("Hay look I'm a dummy script");
      5. }
      6.  
      You can also click "Go Advanced", select your code and then hit the code tags button:
      $codeTagsExample.png



    Tips and tricks
    When debugging, use the Debug class. It is called "Debug" for a reason. It has several useful functions:

    Debug.Log alternatively print
    This function logs a message to the console (seen in image A). Use this to display messages, variables... ect.
    Code (csharp):
    1. Debug.Log("Something");
    2. print("Something else");
    Debug.LogWarning
    Similar to the previous function, LogWarning logs a message in the console. But it is displayed as a warning. Useful for displaying a message that stands out of many Debug.Logs.
    Code (csharp):
    1. Debug.LogWarning("The sky is falling!!!!");
    Debug.LogError
    Again, like previously logs a message to the console. But this time it is a error. If "Error pause" is on in the console (can be seen to the right of "Clear on play" in image A), this will also pause the editor.
    Code (csharp):
    1. Debug.LogError("Somethings wrong dun da dah!");
    Debug.Break
    Pause the editor from code. Helpful for pausing at times you would not be able to pause manually.
    Code (csharp):
    1. Debug.Break();
    Debug.DrawLine and Debug.DrawRay
    These functions can be used for giving visual references to things in game and scene views. However "Gizmos" must be selected in order to view them in the game window:
    $GameView.png
    Code (csharp):
    1.  
    2. //draw a line starting from transform.position and ending at Vector3(0.0,0.0,10.0) in world space
    3. Debug.DrawLine (transform.position, Vector3(0.0,0.0,10.0), Color.red);
    4. //draw a ray (looks the same as a line) from transform.position up one unit relative to transform.position
    5. Debug.DrawRay (transform.position, Vector3.up, Color.green);
    6.  


    Special thanks to:
    The Unity Community
    The TornadoTwins for helping me get started with Unity

    And
    Dabeh
    exiguous
    For their helpful suggestions in the original thread.

    And for assisting in someway in this thread
    Eric5h5 (Well unintentionally anyway)
    anothervenue for boo info
    www.bbcode.org


    Well that is about everything that I can think of at the moment.
    Feel free to comment if you have any suggestions or find anything that needs correction or clarification.