Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question I can't find the error in the docs!!

Discussion in 'Documentation' started by rigel_unity293, Aug 14, 2023.

  1. rigel_unity293

    rigel_unity293

    Joined:
    Jun 29, 2020
    Posts:
    12
    Why is it that I can't find the words in the error, in the documentation itself? Unity wrote the error message, so why haven't they documented it?

    Where do I find the documentation for all the erros produced by the editor for scripts and other things. For exmaple.

    "Can't add script because 'SampleScript' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match."

    Of course you'd replace 'SampleScript' with whatever, so I just omitted it, but I still didnt' get any responses from the documentation.

    It could not even find "Can't add script because"

    How am i supposed to use the documentation this way, if the errors aren't documented? Can anyone help me? Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! See above.

    When you need to debug, use this approach:

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,862
    I mean that error message literally tells you what to do.

    And most errors Unity will throw are either:
    • Regular C# errors
    • An internal engine error that doesn't mean much to you
    • Or an error with an explanation of what to do
    This one tells you what to do. You have compiler errors, or your script name does not match the file name. Go fix it.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,863
    Kurt, he wasn't asking about error codes, he's reading the text of a message. Chill the robo-replies.

    spiney, for people who are not as fluent in English, it is really useful for a page or paragraph to rephrase and explain the general intent in different ways.

    I agree, the error messages should be *somewhere* online. APIs are not just what arguments you pass in, but what errors may be raised. Same goes for compilers. Pretty much any other compiler I have ever used published a compendium of what various error messages meant in a more verbose form. It saves calls to tech support and threads on support forums.
     
  5. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Are you looking for a solution for that error message, or just expressing how you feel about the documentation?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,862
    This isn't a compiler error though. This one pops up when trying to add component when there is a compiler error, or the class and script names don't match.

    Though I agree there should be some documentation page for common errors.
     
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    I don't.
    I think error messages should be self-documented. And this is. It tells you everything you need to know and check. Putting this in the documentation would be only write the same thing down again.

    Which part you don't understand? What is it in this require documentation?
     
  8. rigel_unity293

    rigel_unity293

    Joined:
    Jun 29, 2020
    Posts:
    12
    I ran the debugger but it didn't give me a way to display the clas implementation or the context of the variables therein from intellisense am I missing something in the settings?
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,862
    Ignore Kurt's post, it's not relevant in this context.

    You need to do two things:
    • Ensure there's no compiler errors
    • Make sure the name of your Monobehaviour/Scriptable object class matches the name of your script asset
    That's all there is to this error.
     
    rigel_unity293 likes this.
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    It's in there just not in the way you're thinking of. Here's the relevant text under Anatomy of a Script file.
    https://docs.unity3d.com/Manual/CreatingAndUsingScripts.html

    By the way you should never use that search bar at the top of the docs. Instead you should use Google. Searching for
    site:docs.unity3d.com the file name and class name match
    provided me with the above link. Of course it's no more helpful than the error message as the error message already had everything you needed to know.

    In theory this makes sense and we likely should have an FAQ, but in practice I think the number of those who would look at a manual while simultaneously ignoring an error message is almost zero. Just look at how many threads we have asking about null errors and not using code tags yet both of those are covered by pinned threads.
     
    Last edited: Aug 15, 2023
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Well that's actually the problem... not reading :)

    After OP posts this:

    Which contains this call to action: "Make sure that there are no compile errors and that the file name and class name match."

    The very next line in the original post should be:

    "I do not have any compiler errors and my script is called Foobars.cs and my class is called Foobars."

    Absolutely NOTHING else is relevant until those two points are addressed.

    At that point we have a possible actual issue in play. Until then the standard reply is to RTFM.

    Here's the other catch-all... maybe you just cannot SEE the errors:

    Make sure your log console selector buttons are enabled. See this graphic:

    https://forum.unity.com/threads/cant-add-script-component.632746/#post-4239121

    https://forum.unity.com/threads/update-function-not-working.953477/#post-6215873
     
  12. rigel_unity293

    rigel_unity293

    Joined:
    Jun 29, 2020
    Posts:
    12
    thank you
     
  13. Nefahl

    Nefahl

    Joined:
    Feb 20, 2017
    Posts:
    71
    The error literally tells you all you need to know, what do you expect Unity to additionally write in the Docs regarding the error-message.

    Problem 1)
    You have any compile error --> some file has invalid syntax therefore your written class wont be compiled and thus the unity editor does not know it exists -> can't be added.
    Usually indicated by another error, the erroneous scriptfile doesn't need to be the one you want to add here, a compile error means no script wont be compiled until all those errors are resolved, only scripts that were compiled before are useable BUT without any changes done to the script after the compile-error occured, since they wont be re-compiled, so added class-fields for example wont show up in the inspector.
    Also the editor wont let you start the playmode if there's any.

    These errors are on themselves printed to the console for example a missing semicolon:
    Assets\Scripts\AnyProblematicSourceFile.cs(123(line-number),12(symbol-index): error CS1002: ; expected

    Problem 2)
    Your class name does not match the filename (case matters)

    SampleScript.cs should contain:

    Code (CSharp):
    1. public class SampleScript : MonoBehaviour
    2. {
    3.    // some stuff
    4. }
    what would not work for SampleScript.cs
    Code (CSharp):
    1. public class sampleScript : MonoBehaviour
    2. {
    3.    // some stuff
    4. }
    or

    Code (CSharp):
    1. public class MisnamedScript : MonoBehaviour
    2. {
    3.    // some stuff
    4. }