controller.Move issue

Well I hate to even have to come on and ask for help, but I’ve been lost on this for a few hours now, doing tons of alterations with no success. This script is one I’ve been trying to alter from a JS tutorial, and I managed to clear up the majority of issues I’ve run into except this one. I’m not sure why it’s happening, after much searching I’ve found multiple threads related to people with almost identical structure, and they did not have this issue. If anyone is willing to take a look and give me some input, would be greatly appreciated.

‘CharacterController’ does not contain a definition for ‘Move’ and no extension method ‘Move’ accepting a first argument of type ‘CharacterController’ could be found.
The issue is on line 59

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
public class CharacterController : MonoBehaviour {
//C# cannot define the inititalization of variable interms of another variable...
//Example : private float speed=normalSpeed;
//Instead the value should be set to 0, with the adjustment made in the start function as...
//Example : speed=normalSpeed;
public float normalSpeed=6.0f;
private float speed=0f;
public float runSpeed=12.0f;
private float jumpSpeed=0;
public float gravity=20.0f;
private int walkTime=0;
private Vector3 moveDirection=Vector3.zero;
public static bool grounded=false;
private CharacterController controller;
private CollisionFlags flags;

void Start()
	{
		speed=normalSpeed;
		jumpSpeed=speed * 1.7f;
		
		animation.wrapMode=WrapMode.Loop;
		animation["run"].layer=-1;
		animation["walk"].layer=-1;
		animation["idle"].layer=-1;
		animation.SyncLayer(-1);

		animation["jump"].layer=10;
		animation["jump"].wrapMode=WrapMode.Once;
		animation.SyncLayer(10);

		animation.Stop();
		animation.Play("idle");
	}


void FixedUpdate()
	{
		CharacterController controller = GetComponent<CharacterController>();
		
		if(grounded)
		{
			moveDirection=new Vector3(Input.GetAxis("Horizontal"),0,0);
			moveDirection=transform.TransformDirection(moveDirection);
			moveDirection*=speed;

			if(Input.GetButton("Jump"))
			{
				moveDirection.y=jumpSpeed;
				animation.CrossFade("jump");
			}

		}
		moveDirection.y-=gravity*Time.deltaTime;
		flags = controller.Move(moveDirection * Time.deltaTime);

		grounded=(flags  CollisionFlags.CollidedBelow) !=0;

		if(Input.GetAxis("Horizontal")>.2 || Input.GetAxis("Horizontal")<-.2)
		{
			if(walkTime>40)
			{
				animation.CrossFade("run");
				speed=runSpeed;
			}
			else
			{
				walkTime++;
				animation.CrossFade("walk");
				speed=normalSpeed;
			}
			jumpSpeed=speed*1.7f;
		}
		else
		{
		walkTime=0;
		animation.CrossFade("idle");
		}
	}
}

Perhaps thats happening because you redeclare the controller variable instead of just assigning it in line 43.
If you already declared a variable like

CharacterController controller

you then only assign something to that variable like

controller = GetComponent<CharacterController>()

Even when it’s set to just controller = GetComponent() I get the issue. That’s actually how I had it originally, but added the CharacterController in front to see if that would help, and simply forgot to remove it. Thanks for the reply though, appreciate it.

I could imagine it’s a naming issue. The class you’ve created is called “CharacterController” - which is the exact same name as the CharacterController script from Unity. Now when you call .Move() it looks for this method in the current class - but it doesn’t exist for your own defined class of CharacterController. The actual code of Unity’s CC is hidden, so with your equally named class you might overwrite something…

Edit: in fact… when you do call GetComponent() from your script which is called itself “CharacterController.cs” the engine might just retrieve this (your) script instead of Unity’s CharacterController.

5 Likes

Thanks a bunch, that was actually my issue. I renamed the cs to CharController, and it compiled without an issue. I feel like an idiot for making such a mistake, but that will never happen again. Thanks again.

1 Like

Facing the same issue and your reply from 2013 is still helpful. Thanks a million from 2022.

1 Like

Please use the Like button in the future, it’s what it’s there for! What you did is necro a 9 year old post for everyone to see.

Thanks.

Community Code of Conduct