This question was
closed Nov 03, 2017 at 12:42 AM by
CarlaMiller for the following reason:
Question is off-topic or not relevant
error CS0103 rb doesn't exist in the current context, ligne 8 and 15 , can't fix it
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 * speed);
}
}
Comment
Answer by UnityCoach · Dec 21, 2016 at 01:42 PM
Assign it in Awake instead. You can also add a RequireComponent Attribute to make sure it always has a RigidBody component.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
public float speed;
private Rigidbody rb;
void Awake ()
{
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 * speed);
}
}
Well, I've already try this but it doesn't work , monodevelop still indicates me errors, and the script doesn't work in the Play mode
Can you share the full error message details?
The name 'rb' does not exist in the current context (CSO103) that's all