I'm trying to program a simple Goomba-type enemy in my 2D game that simply walks forward until it hits a wall and then turn around.
I wrote it with a RigidBody2D and changed it's velocity, see code below.
using UnityEngine;
using System.Collections;
public class SmallCritter : MonoBehaviour {
public float moveSpeed;
bool isMirrored = false;
Rigidbody2D rb2D;
void start() {
rb2D = GetComponent ();
}
void FixedUpdate() {
if (isMirrored) {
rb2D.velocity = new Vector2 (moveSpeed, rb2D.velocity.y);
} else {
rb2D.velocity = new Vector2 (-1 * moveSpeed, rb2D.velocity.y);
}
}
However, when starting the game, I get this issue:
"NullReferenceException : Object reference not set to an instance of an object.
I checked and doubled checked the code, but I can't figure out why this doesn't work (the moveSpeed variable is set in Unity).
Can anyone help me?
↧