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

Resolved cs0120 error please help.URGENT

Discussion in 'Scripting' started by metehanoguzhan2801, Aug 18, 2023.

Thread Status:
Not open for further replies.
  1. metehanoguzhan2801

    metehanoguzhan2801

    Joined:
    Jan 27, 2023
    Posts:
    10
    HELLO. Please help me
    in visual studio
    cs0120 error
    I need urgent help
    hata.JPG

    this is the code;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class RaycastActions : MonoBehaviour
    {
    private Ray ray;
    private RaycastHit hit;
    private Camera cam;

    void Start()
    {
    cam = Camera.main;
    }


    void Update()
    {
    ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    if(Physics.Raycast(ray,out hit, 100))
    {
    if(hit.collider.TryGetComponent(out Targetable argetable))
    {
    Targetable.ToogleHighlight(true);
    }
    }
    }
    }
     
    Last edited: Aug 19, 2023
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Please just copy code, and use the first code tab in the post text editor, and then paste it in there, so it's easy to copy/fix/paste for anyone that wants to help.

    Not to be mean, but I don't see the urgency, as this problem is very simple. As you're gonna need a bunch of time to research further into the basics of coding.
     
    Qriva likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    You're just making typos. You don't need us to fix them. You can fix them yourself. Here's how:

    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.

    For future reference, photographs of code are not a thing. Do not TALK about code without posting it. Do NOT retype code. Copy/paste and post code properly. ONLY post the relevant code, and then refer to it in your discussion. Do NOT post photographs of code.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  4. metehanoguzhan2801

    metehanoguzhan2801

    Joined:
    Jan 27, 2023
    Posts:
    10
    I'm posting a first post like this and because I couldn't solve it, I threw it out, this is not the first error I got, I solved all of them but this error is not resolved. I tried many things, but it does not work, if you know how to fix it, help.

    code;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class RaycastActions : MonoBehaviour
    {
    private Ray ray;
    private RaycastHit hit;
    private Camera cam;

    void Start()
    {
    cam = Camera.main;
    }


    void Update()
    {
    ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    if(Physics.Raycast(ray,out hit, 100))
    {
    if(hit.collider.TryGetComponent(out Targetable argetable))
    {
    Targetable.ToogleHighlight(true);
    }
    }
    }
    }
     
  5. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    We all know how infuriating it is, but its also an error code that does explain an issue, and where to look.. google also will give examples.. the number of posts claiming they tried for days/weeks even and yet you type the words in google and look theres an answer cannot be imagined..and as Kurt's post says - 99% of people have not committed all error messages to memory - the error text will nearly always have a line and cahracter position - now sure, occasionally the error is on a line before that, but you have a place to start and its like a 1 in 100 billion chance the compiler has lied.

    also if you read your code as you wrote it, you collect a variable on the lilne above - and whatever language your error maybe - which I dont speak - the code with the error in is almost certainly not what you meant...
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    SO take a look at your code; you try to get a "Targetable" component using "out Targetable argetable" i.e. you name the variable "argetable". Clearly that shouldn't be named like that and should likely be "targetable" (lower-case "t"). You then try to access it by the type as "Targetable.ToogleHighlight(true);" which is wrong. Targetable is the Type, not the variable.

    So, in your current code it would be "argetable.ToogleHighlight(true);" to remove the error.

    With all due respect though, you do need to pay more attention to detail by reading the code in front of you and verifying it. Relying on the compiler to throw errors isn't the best thing to do. If you don't understand C# (which is fine!) then I would recommend following some C# tutorials. The forums do assume a basic working knowledge of C# before you begin. C# is not an exclusive Unity thing, its use is widespread so there are huge amounts of learning resources available to you at your fingertips.

    Thanks.
     
    arkano22 and bugfinders like this.
  7. metehanoguzhan2801

    metehanoguzhan2801

    Joined:
    Jan 27, 2023
    Posts:
    10
    I'm so sorry to take your time, I didn't realize I wrote Aregtable. I know the basic codes. I've been doing this job for about 3 years, but I couldn't see it due to a small mistake, sorry THANKS
     
  8. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    uuhhhhh... what? There definitely must be a translation issue from the looks of things, as that translated to years instead of days. But if you truly meant years, I just imagine your boss double checking your code being very similar to the wtf scene in full metal jacket.

    But missing the
    t
    in
    targetable
    , is a simple thing to overlook , so no problem there.

    But as Melv stated, it is a absolute beginner mistake to not know letter case(BIG LETTERS vs small letters), as I think even in the most basic of code text editors, the color of things is a dead give away. Unless you're typing in NotePad or NotePad++, then you are a far braver man than I.

    However seeing this written as it is, kind of makes me think you still don't grasp letter case. But I'll chock that up to a translation issue, and maybe it happens when you copy/paste from the translator. Which then leads me to believe your code is copy/pasted..

    You'll get further in life, and especially with me, if your honest about things. As I will take the time to make sure you fully understand something(if I fully understand it). :)
     
    Kurt-Dekker and bugfinders like this.
  9. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    3 years?? and you struggled with this? ouch (sorry but.. well not sorry, thats shocking)
     
  10. metehanoguzhan2801

    metehanoguzhan2801

    Joined:
    Jan 27, 2023
    Posts:
    10
    My problem is being in a hurry when writing code. That's it, I had a lot of work and I needed to be fast so I got this error and I couldn't solve it. I have no problem writing code.I also wrote the code completely, it is a very basic code. The problem in the code is not uppercase, not lowercase, instead of writing targetable I write argetable, so I miss that I don't put "t".
     
    Last edited: Aug 19, 2023
  11. metehanoguzhan2801

    metehanoguzhan2801

    Joined:
    Jan 27, 2023
    Posts:
    10
    I've been in game development for three years and I have a lot of projects, but this is no longer a project, No Change in World will be officially released very soon:)
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    It's really more that you haven't learned to read error messages.

    You can say it something else, but it really is just:

    "Read the message and tell your dog what is wrong with the code."

    See my avatar? I'm explaining an error to my dog.

    By explaining it to my dog, that's how I know I understand the error and can fix it.

    Until you understand the error, there is NO POINT in attempting to fix it.
     
    bugfinders likes this.
  13. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,834
    "I don't have time to sharpen my axe, I have all these trees to cut down!"

    Slow down, try to see your own code with fresh eyes, and don't try to claim urgency on other people when the fault is your hurried approach. Slow is smooth, and smooth is quicker.
     
  14. metehanoguzhan2801

    metehanoguzhan2801

    Joined:
    Jan 27, 2023
    Posts:
    10
     
  15. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    over my life time ive written programs in 24 different computer languages.. do i remember them all like i used them 20 minutes ago? no, but frankly for all, logic applies. the error message is clear, this error message is quite clear in where the error is, your minor job was to decide why it didnt like doing that method to that item.. It shouldnt have taken you long even if you hadnt touched the code in 10 years.

    Kurt uses his dog (I have 2 of those, so i concur explaining to them is valid check, others use the humble rubber duck.. My mother to learn things taught her teddy bears (it would have been like 1950/early 1960s for her school exams) its not a new thing, it will reduce the chance of making a fool of yourself in front of others by asking questions you really could have answered, either with following the logic, or google...
     
  16. metehanoguzhan2801

    metehanoguzhan2801

    Joined:
    Jan 27, 2023
    Posts:
    10
    You don't want to understand, yes I was wrong, it was wrong of me to post for such a small thing, but I didn't see it, you can't act like this because I didn't see such a letter, it would be shameful.
     
  17. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    I did not attack you but gave you advice on avoidng making an ass of yourself - which frankly you did.
     
    Kurt-Dekker likes this.
  18. metehanoguzhan2801

    metehanoguzhan2801

    Joined:
    Jan 27, 2023
    Posts:
    10
    We all make mistakes (including you) we apologize for mistakes and the subject is closed, I apologized for not seeing it and the subject had to be closed, but you come and insult me, it's really not worth it for a letter.
     
  19. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    I only in my last post called you out on making an ass of yourself, and only because you are accusing me of things I did not do.. Dont lash out at others for a dumb mistake you feel stupid for.. We didnt do it.. you did.
     
  20. metehanoguzhan2801

    metehanoguzhan2801

    Joined:
    Jan 27, 2023
    Posts:
    10
    I am not going to prolong the issue anymore because you obviously will not understand me and you will continue to attack and insult, SHAME what you are doing
     
  21. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    then dont start slinging mud at people.. because they may sling back..
     
  22. metehanoguzhan2801

    metehanoguzhan2801

    Joined:
    Jan 27, 2023
    Posts:
    10
    I am not saying anything to them, they helped me, thank you again, I am only saying to those who insulted me as if it was my fault, as if I blew up the world, whoever takes offense, I only asked for help.
     
Thread Status:
Not open for further replies.