Why does this copied and pasted code not work somewhere else?

So i pasted this in, changed the name of the class, and its all red. why? the term game object and other things aren’t recognized. it doesn’t recognize “monobehaviour” at the top

	// Basic properties
	public int Health;
	public float Speed;
	
	// Basic distance floats
	private float Distance;
	public float GeneralDistance;
	public float SeparateDistance;
	public float MeetDistance;
	
	// Layer appearance, depending on where the leader is in relation to the follower
	private int InFrontOfLayer = 21;
	private int UnderneathLayer = 18;
	
	// Assigns a game object for the follower to follow
	public GameObject Leader;
	
	// Tells the follower whether or not to follow the leader
	public bool Following;
	
	void Start () {
	}
	
	void Update () {
		// Makes the value of distance equal to the space between the leader and the follower
		Distance = Vector2.Distance (Leader.rigidbody2D.transform.position, rigidbody2D.transform.position);
		
		// If the distance between the leader and follower is less than or equal to the distance required for the follower to follow
		if (Distance <= MeetDistance)
		{
			// The follower will follow
			Following = true;
		}
		// If the  distance between the leader and follower is greater than or equal to the distance required for the follower to stop following
		if (Distance >= SeparateDistance)
		{
			// The follower will not follow
			Following = false;
		}
		// If the follower is following
		if (Following == true)
		{
			// If the leader's x value is greater than the sum of the follower's x value and the general distance set
			if (Leader.rigidbody2D.transform.position.x > rigidbody2D.transform.position.x + GeneralDistance)
			{
				// Move the follower to the right
				rigidbody2D.transform.position += Vector3.right * Speed * Time.deltaTime;
			}
			// If the leader's x value is less than the difference between the follower's x value and the general distance set
			if (Leader.rigidbody2D.transform.position.x < rigidbody2D.transform.position.x - GeneralDistance)
			{
				// Move the follower to the left
				rigidbody2D.transform.position += Vector3.left * Speed * Time.deltaTime;
			}
			// If the leader's y value is greater than the sum of the follower's y value and the general distance set
			if (Leader.rigidbody2D.transform.position.y > rigidbody2D.transform.position.y + GeneralDistance)
			{
				// Move the follower up
				rigidbody2D.transform.position += Vector3.up * Speed * Time.deltaTime;
			}
			// If the leader's y value is less than the difference between the follower's y value and the general distance set
			if (Leader.rigidbody2D.transform.position.y < rigidbody2D.transform.position.y - GeneralDistance)
			{
				// Move the follower down
				rigidbody2D.transform.position += Vector3.down * Speed * Time.deltaTime;
			}
			
			// If the leader's y value is less than or equal to the follower's y value
			if (Leader.rigidbody2D.transform.position.y <= rigidbody2D.transform.position.y)
			{
				// Make the leader appear in front of the follower
				Leader.renderer.sortingOrder = InFrontOfLayer;
			}
			// If the leader's y value is greater than the follower's y value
			if (Leader.rigidbody2D.transform.position.y > rigidbody2D.transform.position.y)
			{
				// Make the leader appear underneath the follower
				Leader.renderer.sortingOrder = UnderneathLayer;
			}
		}
	}
}

This type of problem is normally a parsing error. Things to check.

  • You have a .cs file and not a .js file
  • There will be errors in the console. Check them and resolve
  • There is nothing wrong before the class declaration
  • monobehaviour does not exist. Try MonoBehaviour instead

And for the last time, post the entire relevant script. Please edit your OP to contain the entire script that is causing problems.

Probably because it’s missing a lot of variables and do you have a leader object?

You’re missing the Usings.

Add

using UnityEngine;

at the very top.

And also it’s not encapsulated in a class. After adding the using, put the rest inside:

public class YourClassName : MonoBehaviour {
//Your class code here.
}