New to unity, can't figure out why I am getting an error message

So I’m figuring out unity by just going in and searching up whatever i don’t understand, and im trying to avoid tutorials. Maybe that’s a bad idea, but following tutorials is boring.
I’m making a 2d character controller, and i’m implementing a thing that will check if you’re on a platform so you can jump. the jump checker itself works, but i cannot get my player controller to get the variable from the other script.

The error message i’m getting is PlayerController.cs(14,5): error CS0246: The type or namespace name ‘CanJump’ could not be found (are you missing a using directive or an assembly reference?)

also apologies in advance for my probably terrible code

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

public class PlayerController : MonoBehaviour
{
    public Rigidbody2D myRigidbody;
    public float MoveSpeed;
    public float MaxSpeed;
    public float JumpHeight;

   
    GameObject JumpCheck;
    CanJump Jumpable;

 






    // Start is called before the first frame update
    void Start()
    {
        GameObject JumpCheck = GameObject.Find("JumpCheck");
    }
   
    // Update is called once per frame
    void Update()
    {
        Jumpable = JumpCheck.GetComponent<CanJump>();

       
            //move left
        if(Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.LeftArrow))
        {
            myRigidbody.velocity = myRigidbody.velocity + Vector2.left*MoveSpeed * Time.deltaTime;
            if (MoveSpeed > MaxSpeed) {MoveSpeed=MaxSpeed;}
        }

            //jump
        if(Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.Space) | Input.GetKey(KeyCode.UpArrow) && Jumpable==true)
        {
            myRigidbody.velocity = myRigidbody.velocity + Vector2.up*JumpHeight * Time.deltaTime;
        }

            //move right
        if(Input.GetKey(KeyCode.D) | Input.GetKey(KeyCode.RightArrow))
        {
            myRigidbody.velocity = myRigidbody.velocity + Vector2.right*MoveSpeed * Time.deltaTime;
            if (MoveSpeed < -MaxSpeed) {MoveSpeed = -MaxSpeed;}
        }




    }


   
}

What am i doing wrong?

Well then be prepared to not get particularly far. You are going to have to follow some lessons to understand the fundamentals of both C# and Unity before you can start wandering off on your own.

I recommend starting here: https://learn.unity.com/pathway/junior-programmer

To answer your question, you have declared CanJump as a type and you don’t have a type matching it defined anywhere. Is it meant to be a boolean value perhaps?

These answers can be learned by learning some basic C# principles.

3 Likes

And if following tutorials is boring you should try answering simple questions from people who can’t be troubled with following some tutorials. Now that’s boring.

1 Like

Sorry for the late answer, winter break ended and school started up again for me.
I have actually watched a bunch of tutorials and brackeys and whatnot, but i just cannot get myself to stick. Ive done some of the lessons that unity gives you as well, but dropped it after a while.
Ive kinda just decided to try and recreate the first level of mario and teach myself anything i need to know.

Anyways, in regards to the question, i have another script on another object that has the CanJump bool. That script works just fine and its public and all but i just can’t get this script to reference it.
Earlier, i had a different error which was fixed by following what this guy did ttps://forum.unity.com/threads/getcomponent-returns-null-solved.543930/ and im pretty sure i copied them correctly and their problem seems to be fixed, but yknow, i still have my error

Looks like you’re just either making lazy typos or assuming incorrectly. That’s gonna waste a LOT more time.

In case you rethink your position on tutorials, I suggest this two-step process.

Otherwise you will be figuring it all out on your own, and the domain knowledge space is so vast that not even professional software engineers avoid tutorials, so I would be amazed if a beginner could do without them.

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! 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.

Line 14 column 5 you have declared a variable of type CanJump but Visual Studio cannot find it. One common explanation is that CanJump is declared within a namespace that you are not using in this code, another one is that it failed to compile because of an error in its code.

First you should simply tidy up the code. It won’t fix errors but it makes it easier to digest and you return some empty lines back into the empty line pool so others can have some.

The definition you posted related to CanJump is not a bool it is a class name. JumpCheck doesn’t get assigned to the property because you have assigned it to a local variable in Start().

Serious recommendation. Don’t try to write a game yet. Try some C# in a simple console app and make sure you can define a class, subclass something, instantiate a class, set properties, etc. Doing it all in the context of Unity will only slow your learning down.

You don’t need to know everything but you do need to figure out how to debug your own code and making errors and figuring out the problem is the way you do that.

Oh wow, yeah, nobody should need to look at expressions like this one:

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums

ALSO, your use of Time.deltaTime to set a velocity is fundamentally flawed, another thing that would have come to you by tutorials because that’s simply not how to do it, not to mention this is WAY too hairy a line of code in its own right:

And this:

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

More information: https://discussions.unity.com/t/899843/12

Not only that but by doing GameObject JumpCheck = in Start(), you just made a new local variable that is COMPLETELY different from your class variable, so your class variable will be null and you will get a NullReferenceException.

Set yourself up for success.

If seeing and studying success is boring to you, perhaps you have selected the wrong pursuit?