Search Unity

Unity error type or namespace seems incorrect and cannot be resolved.

Discussion in 'Scripting' started by Kenthria2, Jan 13, 2019.

  1. Kenthria2

    Kenthria2

    Joined:
    May 15, 2018
    Posts:
    9
    Error: "Assets\Scripts\CarMovement.cs(17,12): error CS0246: The type or namespace name 'AxisInput' could not be found (are you missing a using directive or an assembly reference?)"
    .
    I am trying to use GetComponent to retrieve a variable from another script. The script is named AxisInputUI. I have tried using the most direct method to get it and got the same error. I tried to access the component script via a variable to no avail.
    .
    Below are the parts of the script that look for the component.
    public AxisInput axisinput;
    public void Start()
    {
    GameObject Top = GameObject.FindWithTag("TopButton");
    axisinput = Top.GetComponent<AxisInputUI>();
    }
    //Needs to get the variable 'value' from the script AxisInputUI
    //And then do IF variable 'value' > 0 ....
    void Update ()
    {
    if (axisinput != null)
    {
    value = axisinput.value;
    }
    }
    I have also done what is suggested here with no luck:
    https://stackoverflow.com/questions...4pQmbqSrJ3pDFoy1M9DBis9niyHtWX04NEHGL9L7y_y5w
    Thank you for any help. With this issue I am unable to add mobile controls.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    You're using `AxisInput' on the variable declaration, and `AxisInputUI' in your GetComponent call. Sounds like `AxisInput' just needs to be changed to `AxisInputUI'
     
  3. Kenthria2

    Kenthria2

    Joined:
    May 15, 2018
    Posts:
    9
    I tried that originally and got the same error, except the error was specifically to AxisInputUI.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Well, your code is almost certainly wrong if you have something like this:

    Code (CSharp):
    1. public AxisInput axisinput;
    2. public void Start()
    3. {
    4. GameObject Top = GameObject.FindWithTag("TopButton");
    5. axisinput = Top.GetComponent<AxisInputUI>();
    6. }
    The only way that would be correct would be if there really are two classes, AxisInput and AxisInputUI, and AxisInputUI inherits from AxisInput. I'm assuming that's not the case, but you tell me: Do you have one class named AxisInputUI? Or do you really have two classes in your project, named AxisInput and AxisInputUI, where AxisInputUI inherits from AxisInput?

    After you sort that out, if you still get compiler errors of the type "The type or namespace name 'AxisInputUI' could not be found", it likely means one of the following:
    • You copy/pasted this code from somewhere, but didn't copy the code for AxisInputUI into your project.
    • The AxisInputUI class is in another namespace, which you haven't included here.
    Maybe you can include the code for AxisInputUI here. Maybe it has a 'namespace' declaration towards the top of the file? If so, you'll need to add a using statement to include that namespace in your class.
     
    Kenthria2 likes this.