I try to make my character move by using CharacterController but the problem is in the run time it tell me that the character controller does not match with any anything and the character do not move at all, so is there any falt in the code:
I haven’t read your full code, but you can’t make an instance of a characterController with the new keyword like any other normal class. For all the unity components you should use the GameObject.AddComponent(“Type_Name”) method. Use it like you would use the new keyword:
And then you can call any of the components in controller as you normally would, like controller.Move() for example.
Notice, though, that the object’s inspector will only include the CharacterController in runtime and it won’t be saved when closing the editor. So you’d probably want to add that controller from the editor and then get it through code with GameObject.GetComponent(), you can use it like this:
This may come in handy especially when having to set the values for your component, like the dimensions for the collider, or any other value. You can then modify them in runtime and forget about modifying them through script. Much faster and simple.
That second method will not work if you have no character controller in that object before running the game and will result in a NullReferenceException. There is a way to force the object to have a required component through scripting, but I don’t remember it right now, a simple search on it may provide many good results though.
You can’t create new components (you can’t ever do “new CharacterController”, or any other type of Component). You have to use AddComponent.
Unless you’re changing things into characters on the fly, you’ll want to add the CharacterController component in the editor, not through script. Then your code will work (if you take out the new line, and have the controller on the same object as this script).