- Home /
Script I copied from Movement tutorial doesnt work
Hi, newbie here. I followed this tutorial: "Moving the Player" "https://unity3d.com/es/learn/tutorials/projects/roll-a-ball/moving-the-player" ,and copied the script, this is my script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement);
}
}
And is attached to the ball wich has "Player" as name. When I try to play, the console window it says: "The referenced script on this Behaviour is missing!". Twice.
On the inspector window, under the script that has "movementBall" as name, it says: "The associated script can not be loaded. Please fix any compile errors and assign a valid script". I made sure that the bal has the rigid body component.
Answer by Gardes · Jun 12, 2015 at 02:24 AM
It seems your script is named wrong.
public class PlayerController : MonoBehaviour {
If your class is "PlayerController.cs" your script has to be named as PlayerController.cs and not as "movementBall.cs". So finally scriptname has to be same like classname and vise versa. Name your script PlayerController.cs or change the line I posted before to:
public class movementBall : MonoBehaviour {
Answer by unity_037C2224FC8B1C22580C · Aug 20, 2021 at 11:00 AM
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour { public float speed = 0;
private Rigidbody rb;
private float movementX;
private float movementY;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
private void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
}
}
Your answer
Follow this Question
Related Questions
How do I slow down while Im Sliding? 0 Answers
New to Unity & Scripting, How Do I Create a Movement System Thats About Building Up Speed? 0 Answers
Coroutine fires once after destroying gameobjects in a scene and recreating new objects 0 Answers