- Home /
Player turns but won't move forward
Hi there.
I just started with Unity today, and I started by watching all the tutorials. Now I'm following a little starting project guide I found (really basic). But I already have a problem with the SimpleMove function. So far I only have a plane for floor, and a sphere for player. My player will turn when I press the left/right buttons, but he won't move to the front or the back with the up/down buttons. I had some trouble with the simplemove function, but after looking it up in the reference I think the code is now okay. However, the problem is still there, my player will turn but won't move.
I even put my code in comment and tried to see if copying the code from the SimpleMove reference page of the website worked, but it still didn't work. So it doesn't seem to be a fault in the coding, but I don't really know where else to look... Maybe I need to import another package to the script, or there's some other component I need to add in the Inspector or something like that? (I've already done the charactercontroller)
using UnityEngine;
using System.Collections;
public class Player_script : MonoBehaviour {
public int speed = 3;
public int rotatespeed = 3;
void Start () {
}
void Update () {
CharacterController controller = GetComponent<CharacterController>();
transform.Rotate(0, Input.GetAxis("Horizontal") * rotatespeed, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
float curspeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curspeed);
}
}
While typing this, I just wanted to check if I really wasn't missing something stupid, and I just saw that there's an error when I press play:
"NullReferenceException: Object reference not set to an instance of an object Player_script.Update () (at Assets/Player_script.cs:19)"
I tried looking it up, but I didn't find anything relevant. The "at Assets/Player_script.cs:19" makes me think that the problem is on line 19 ( controller.SimpleMove(forward * curspeed); ), but I really don't find anything wrong with it?
I hope someone can help me soon!
Thanks in advance!
Corove
Do you have a character controller attached to your Player?
You should also look at the Character controller script to see what variable type the function Simple$$anonymous$$ove needs to work
Edit: oops just noticed line 14 you define controller. Do you have a CharacterController game component attached to this gameobject?
I just started Unity up to continue looking for the problem, and it worked... I don't really get it...
$$anonymous$$aybe something didn't load right, or I needed to refresh?
Anyway, it works now. I just hope I don't run into this often.
Thanks for the quick answers though!
Answer by ZachAttack9280 · Oct 27, 2015 at 03:09 AM
GUYS I FIGURED IT OUT IDK HOW BUT DO I DID THIS.
I closed unity completely
Shut off computer
turn back on
log back into unity
open your project
then it should be working again! this is what i did and it worked! goodluck:)