"CharacterController is inaccessible due to its protection level"?

This literally never happened to me.

I was following a tutorial on how to make footsteps, and when I got to the “isGrounded” part in my script, it turned red. In the console it said that the CharacterController (which is another script) doesn’t have a definition for “isGrounded”. And it also said what I wrote in the title.

I googled it, and the only suggestion I could find was declaring stuff as public. But obviously that’s specific to every single code.

Here’s the CharacterController script:

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

public class CharacterController : MonoBehaviour {

    bool isGrounded = true;
    public float speed = 10.0f;

    // Use this for initialization
    void Start () {
        Cursor.lockState = CursorLockMode.Locked;
    }
   
    // Update is called once per frame
    void Update () {

        float translation = Input.GetAxis ("Vertical") * speed;
        float straffe = Input.GetAxis ("Horizontal") * speed;
        translation *= Time.deltaTime;
        straffe *= Time.deltaTime;

        transform.Translate (straffe, 0, translation);

        if (Input.GetKeyDown ("escape")) {
            Cursor.lockState = CursorLockMode.None;
        }
    }
}

And here is the script from the tutorial:

using UnityEngine;
using System.Collections;

public class Footsteps : MonoBehaviour {

    CharacterController cc;
   
    void Start () {
        cc = GetComponent<CharacterController>();
    }
   
    void Update () {
        if(cc.isGrounded == true && cc.velocity.magnitude > 2f && audio.isPlaying == false) {
            audio.volume = Random.Range(0.8f, 1);
            audio.pitch = Random.Range(0.8f, 1.1f);
            audio.Play();
        }
    }
}

Anyone got any idea what the problem is in the CharacterController?

Just make isGrounded a public variable

Also, you can’t access a transform velocity this way. Your script cc doesn’t know by itself that you attached à Rigidbody to the GameObject. You need to get its Rigidbody first :

cc.transform.GetComponent().velocity

1 Like

That solved the “isGrounded” thing. But I just realized I still have a lot of other red areas, and errors. I’m just going to post them as screenshots, so people can see exactly what’s wrong now.

Here is the tutorial script in MonoDevelop:
3057381--229573--upload_2017-5-4_16-25-34.png

And here are the errors in the console:
3057381--229577--upload_2017-5-4_16-27-6.png

Hope you (or someone else) can help me with these.

Unity has a class named CharacterController: Unity - Scripting API: CharacterController

If your script has a class of the same name, you’re going to run into conflicts where Unity thinks you want one but you actually want the other. Rename your script to something else.

1 Like

Thanks. That got rid of the magnitude issue.

Now I’m just left with the “audio” ones. Like “isPlaying” and stuff.
Any idea what to do about those?

I think I remember having audio issues in one of my previous projects, and having to do “GetComponent”, but I’m not sure. And I don’t have the files for that project anymore.

You would need to have a reference to the AudioSource.

Although, if the tutorials is making that many errors, I would scrap it and find another. It cannot be teaching you anything good.

The tutorial was made in 2014, and for him it worked fine when he did it.

Then use Unity 4.x. Back then you could access a rigidbody and audiosource without a reference.

Why would I go back a whole 1.x version just to make coding sounds slightly easier?

Here’s my script so far:

using UnityEngine;
using System.Collections;


[RequireComponent(typeof(AudioSource))]
public class Footsteps : MonoBehaviour {

    CharacterController cc;
    public AudioSource WalkingOnDirtSoundEffect;
   
    void Start () {
        cc = GetComponent<CharacterController>();
        WalkingOnDirtSoundEffect = GetComponent<AudioSource>();
    }
   
    void Update () {
       
        if(cc.isGrounded == true && cc.velocity.magnitude > 2f && AudioSource.isPlaying == false) {
            AudioSource.volume = Random.Range(0.8f, 1);
            AudioSource.pitch = Random.Range(0.8f, 1.1f);
            AudioSource.Play();
        }
    }
}

I’m not getting any red anymore, but I still have 4 errors in the console. They are all “object reference” type errors.

What do I do now? I thought I already made a reference on line 13.

WalkingOnDirtSoundEffect.volume ect not AudioSource…

1 Like

Now it says there’s no “CharacterController” attached to the player gameobject, but a script is trying to access it.
But when I add a CharacterController component to the player and run the game, it freaks out and I get launched into the air.

Why does Unity have to be so freaking difficult to deal with? This is harder than learning to read, walk, talk and write combined.

As I said, use Unity 4. A lot has changed and code has to be delt with differently. That is why when you import a unity 4 project into 5, it goes through your scripts and changes them for you.

So… Make a project in Unity 4, put the code in, then open that project in unity 5 and take the code out? That’s all I have to do now?

Oooor, are you telling me to use Unity 4 for the whole project?

Go through the entire tutorial in unity 4 as that is what it was made for. When it is finished and working, you can import it and see what gets changed. Using git or source control would make that quick and easy.

1 Like

Instead of using "AudioSource. " (etc) on those few lines, use the variable that you assigned in Start()
that was GetComponent
WalkingOnDirtSoundEffect I think you called it :slight_smile:

You’re trying to access non-static members using the class, not the instance/object (ie: variable).

And your one other red line error about the character controller, you’d have to repost the line in question to see what that’s about ; I couldn’t guess from this thread at this point