Search Unity

These errors keep happening

Discussion in 'Scripting' started by UrMomDerpThanoFish, Oct 18, 2019.

  1. UrMomDerpThanoFish

    UrMomDerpThanoFish

    Joined:
    Oct 18, 2019
    Posts:
    7
    Assets\Scripts\Pull.cs(6,12): error CS0246: The type or namespace name 'Rigidbody2D' could not be found (are you missing a using directive or an assembly reference?)

    and

    Assets\Scripts\Pull.cs(3,21): error CS0246: The type or namespace name 'MonoBehaviour' could not be found (are you missing a using directive or an assembly reference?)

    Does anyone know how to fix it, if so please tell me :D
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    This should really go in the Scripting section, since a) it's a scripting question and b) this is specifically not a support section. ;)

    The error message is already telling you what the problem almost certainly is. At the top of a C# file is usually a short list of "using xyz;" statements. These tell the compiler what bits of code outside of your project the current file needs to use. If you create a new script file in Unity it has a default list at the start. I suggest starting by checking what's in that list which isn't in yours, and seeing if that fixes the issue.

    (Or, if you don't care about learning, just copy-paste the default list of using statements into the start of your broken script file, making sure to remove any duplicates.)

    For future reference: when asking for help with code, please use [ code ] tags to post the code you're having issues with. That way we don't have to guess what code caused the error to pop up, and you're more likely to get useful help.
     
  3. UrMomDerpThanoFish

    UrMomDerpThanoFish

    Joined:
    Oct 18, 2019
    Posts:
    7
    Ok thank you, im new to Unity so sorry if my question was very basic
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    It is nothing wrong if the question is very basic. Just as "angrypenguin" said, it is better to ask in the matching section. Also don't be afraid :rolleyes:
     
    angrypenguin likes this.
  5. UrMomDerpThanoFish

    UrMomDerpThanoFish

    Joined:
    Oct 18, 2019
    Posts:
    7
    Oh come on now this error showed up

    Assets\Scripts\BasicStickMan.cs(38,76): error CS0103: The name 'ratio' does not exist in the current context
     
  6. UrMomDerpThanoFish

    UrMomDerpThanoFish

    Joined:
    Oct 18, 2019
    Posts:
    7
    My code is

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Pull : MonoBehaviour
    {
    public float force = 70;
    public Rigidbody2D rg;
    // Start is called before the first frame update
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
    if (Input.GetKey(KeyCode.Mouse0))
    {
    Vector2 dir = rg.position - (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
    dir = dir.normalized;
    rg.AddForce(-dir * force * Time.deltaTime * 1000);
    }
    }
    }
     
  7. UrMomDerpThanoFish

    UrMomDerpThanoFish

    Joined:
    Oct 18, 2019
    Posts:
    7
    Does anyone know how to fix the error?
     
  8. This is not code, it's some random text. Use code tags when you post your code.
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    Post your relevant code. This is not it. Your error says
    BasicStickMan.cs(38,76)
    So post your BasicStickMan.cs code. And state which one is the 38th line.

    And please, use the Scripting forum section.
     
    vakabaka, angrypenguin and Antypodish like this.
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Yep. To clarify, @UrMomDerpThanoFish, the first part of a compiler error says what file, line and character the compiler got confused on. That's usually where the error is. So the file is "...BasicStickMan.cs", and you need to look on line 38. The character number isn't always useful, but can point you to which part of a statement is busted.

    The part of the message after that is the compiler telling you why it's confused. In this case you're referring to a name, "ratio", which it doesn't understand. Common causes for that are:
    - A typo in the name. You need to use it exactly as it is written where you defined it.
    - Using it out of scope. Either it's in a different file, or a smaller scope within the same file. Look up "variable scoping" to learn about this.

    What program are you using to edit your code? I recommend something like Visual Studio, because it'll give you red underlines for most errors in real time as you're writing code. Also you can double-click on errors in the Unity console and it can open supported code editors to the right file and line for you.

    No sweat. It's better to ask too many questions than not enough, as long as you're paying attention to answers.
     
    Last edited: Oct 19, 2019
    vakabaka likes this.