How to make a sprite walk on its own?

I want to make a sprite move around on it’s own, like an npc, is this possible? I’ve tried a few scripts but they were from 2013 I realised after using them and testing them on monodevelop and to see there was 5 errors, if anybody knows if/how to do this I’d love if you were to give me the script, and is there any component I have to give to the object?

Ah okay, of course I could send it, here it is:

{ { void Update () {
comprobarDistancia();
if (walk)
Walk ();
else if(!walk && !isWaiting){
StartCoroutine(Wait());
}

 }

IEnumerator Wait(){
isWaiting = true;
animation.CrossFade (“idle”);
yield return new WaitForSeconds (5.0f);
walk = true;
isWaiting = false;

You can do the following steps and write the following code and see if it works for you…

STEPS

  1. Create a gameObject in the scene
  2. Add a rigidbody component to it
  3. Add a 2d collider to it (example: BoxCollider2D)
  4. Create a new script call it NpcBehaviour
  5. Write the following code…

CODE
`
using UnityEngine;

public class NpcBehaviour : MonoBehaviour
{
public float speed;
public float moveRate;

public int dirX;
public int dirY;

private float moveCounter;

private new Rigidbody2D rigidbody2D { get { return GetComponent<Rigidbody2D>() ?? default(Rigidbody2D); } }

private void Update ()
{
	if (rigidbody2D)
	{
		if (moveCounter > moveRate)
		{
			ChangeDirection();				
			moveCounter = 0f;
		}
		
		Vector2 vel = new Vector2(dirX * speed, dirY * speed);
		
		rigidbody2D.velocity = Vector2.Lerp(rigidbody2D.velocity, vel, Time.deltaTime * 10f);
		
		moveCounter += Time.deltaTime;
	}
}

private void ChangeDirection ()
{
	dirX = Random.Range(-1, 1); // -1 or 0 or 1
	dirY = Random.Range(-1, 1); // -1 or 0 or 1
}

}
`

FINAL STEP

  1. Attach the script to the desired game object

Hope this helps!