Hi, can you help me please?
I have problem, with this script:
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
void Update()
{
// Get the rigidbody component
r2d = GetComponent<Rigidbody2D>;
// Move the spaceship when an arrow key is pressed
if (Input.GetKey("right"))
r2d.velocity.x = 10;
else if (Input.GetKey("left"))
r2d.velocity.x = -10;
else
r2d.velocity.x = 0;
}
}
Unity is write this errors:
Assets/Controller.cs(9,9): error CS0103: The name r2d' does not exist in the current context Assets/Controller.cs(13,13): error CS0103: The name r2d’ does not exist in the current context
Assets/Controller.cs(15,13): error CS0103: The name r2d' does not exist in the current context Assets/Controller.cs(17,13): error CS0103: The name r2d’ does not exist in the current context
Thank you.
I took your advice, but now is next errors:
Assets/Controller.cs(11,21): error CS0428: Cannot convert method group GetComponent' to non-delegate type UnityEngine.Rigidbody2D’. Consider using parentheses to invoke the method
Assets/Controller.cs(15,16): error CS1612: Cannot modify a value type return value of UnityEngine.Rigidbody2D.velocity'. Consider storing the value in a temporary variable Assets/Controller.cs(17,17): error CS1612: Cannot modify a value type return value of UnityEngine.Rigidbody2D.velocity’. Consider storing the value in a temporary variable
Assets/Controller.cs(19,17): error CS1612: Cannot modify a value type return value of `UnityEngine.Rigidbody2D.velocity’. Consider storing the value in a temporary variable
Code now:
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour
{
void Update()
{
// Get the rigidbody component
Rigidbody2D r2d = GetComponent<Rigidbody2D>;
// Move the spaceship when an arrow key is pressed
if (Input.GetKey("right"))
r2d.velocity.x = 10;
else if (Input.GetKey("left"))
r2d.velocity.x = -10;
else
r2d.velocity.x = 0;
}
}