Question is off-topic or not relevant
An object reference is required for non-static field, method, or property
I started to making my own game. I found the moving script tutorial for unity 4. But I have unity 5. And I got this error in the following script. Please, help me?
using UnityEngine;
using System.Collections;
public class ControllScript : MonoBehaviour
{
public float maxSpeed = 10f;
private bool isFacingRight = true;
private Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
}
private void FixedUpdate()
{
float move = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 && !isFacingRight)
Flip();
else if (move < 0 && isFacingRight)
Flip();
}
private void Flip()
{
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Answer by doublemax · Oct 07, 2016 at 11:58 PM
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
You can't use rigidbody2D like this any more. You need to do the same like for the Animator component: Add a variable to the class the get the component in Start()
private RigidBody2D rigidbody2D;
***
rigidbody2D = GetComponent<RigidBody2D>();
Follow this Question
Related Questions
Error upon rebooting Unity - "Associated script cannot be loaded" How can I resolve this? 2 Answers
Script error after upgrade 1 Answer
Is there a way to make a script activate when you look at an object? 1 Answer
Move a separate object forward on collision between particle system and another object 0 Answers
Is there possibility to have 1 Script for Different SceneSwitches? 2 Answers