Moving Platform Help (c#)

Hey boys and girls,

So I made this simple script after not using unity for 9 months and I really… REALLY RUSTY with everything. I know I’ve made a script like this before but I can’t fix it. It’s a simple script, I want the object to move between 2 waypoints (A & B). this is the error I get:

NullReferenceException: Object reference not set to an instance of an object
MovingPlatforms.Update () (at Assets/Game scripts/Enviroment Scripts/Platforms/MovingPlatforms.cs:13)

and this is my code:

using UnityEngine;
using System.Collections;

public class MovingPlatforms : MonoBehaviour 
{
	public Transform[] Waypoints;
	public bool move = false;
	public float speed = 5;
	

	void Update () 
	{
		if(transform.position.y < Waypoints[1].transform.position.y && move == false)
		{
			this.transform.position = Vector3.Slerp(transform.position, Waypoints[1].transform.position, speed * Time.deltaTime);
			move = true;
		}
		else if(transform.position.y > Waypoints[2].transform.position.y && move == true)
		{
			this.transform.position = Vector3.Slerp(transform.position, Waypoints[2].transform.position, speed * Time.deltaTime);
			move = false;
		}
	
	}
}

Any help would be greatly appreciated.

I figured out what my issues were and I made the code like 100% easier and more usable for multiple waypoints. I’m going to include it as a learning resource incase someone stumbles accross this in the future:

using UnityEngine;
using System.Collections;

public class MovingPlatforms : MonoBehaviour 
{
	public Transform[] Waypoints;
	public float speed = 2;

	public int CurrentPoint = 0;

	void Update () 
	{
		if(transform.position.y != Waypoints[CurrentPoint].transform.position.y)
		{
			transform.position = Vector3.MoveTowards(transform.position, Waypoints[CurrentPoint].transform.position, speed * Time.deltaTime);
		}

		if(transform.position.y == Waypoints[CurrentPoint].transform.position.y)
		{
			CurrentPoint +=1;
		}
		if( CurrentPoint >= Waypoints.Length)
		{
			CurrentPoint = 0; 
		}
	}
}

I tweaked your script (and am currently successfully using it… thank you).
Each waypoint can now transition between position, rotation, and scale to include the entire Transform. Great job on the simple script!

Here’s my version:

*** I am using “void FixedUpdate()”, but you can also use the normal “void Update()” ***

using UnityEngine;
using System.Collections;

public class MovingPlatform : MonoBehaviour 
{
	public Transform[] Waypoints;
	public float moveSpeed = 3;
	public float rotateSpeed = 0.5f;
	public float scaleSpeed = 0.5f;
	public int CurrentPoint = 0;
	
	void FixedUpdate () 
	{
		if(transform.position != Waypoints[CurrentPoint].transform.position)
		{
			transform.position = Vector3.MoveTowards(transform.position, Waypoints[CurrentPoint].transform.position, moveSpeed * Time.deltaTime);
			transform.rotation = Quaternion.Lerp (transform.rotation, Waypoints[CurrentPoint].transform.rotation, rotateSpeed * Time.deltaTime);
			transform.localScale = Vector3.Lerp (transform.localScale, Waypoints[CurrentPoint].transform.localScale, scaleSpeed * Time.deltaTime);
		}
		
		if(transform.position == Waypoints[CurrentPoint].transform.position)
		{
			CurrentPoint +=1;
		}
		if( CurrentPoint >= Waypoints.Length)
		{
			CurrentPoint = 0; 
		}
	}
}

Hi, that’s my script for simple moving platform, wrote it yesterday.

using UnityEngine;
using System.Collections;

public class MovingPlatform : MonoBehaviour 
{
	public float speed = 0.5f, reductionFactor = 1;
	public bool moveOnTouch = false;

	Transform toPoint, playerParent;
	Vector3 fromPos, toPos;
	bool forward = true, touch = false;
	float distFrom, distTo, moveSpeed, lowSpeedDist;

	void Awake() {
		toPoint = transform.Find ("toPoint");
		fromPos = transform.position;
		toPos = toPoint.transform.position;
		lowSpeedDist = Mathf.Abs (fromPos.x - toPos.x) * 0.15f;
		toPoint.parent = null;
	}

	void FixedUpdate() {
		if (!moveOnTouch) {
			Move ();
		} else if (touch) {
			Move ();
		}
	}

	void Move() {
		distFrom = Mathf.Abs(transform.position.x - fromPos.x);
		distTo = Mathf.Abs(transform.position.x - toPos.x);
		reductionFactor = (reductionFactor > 0 ? reductionFactor : 1);
		moveSpeed = (distFrom < lowSpeedDist || distTo < lowSpeedDist ? speed / reductionFactor : speed);

		transform.position = Vector3.MoveTowards (transform.position, 
			(checkPos () ? toPos : fromPos),
			moveSpeed * Time.deltaTime);
	}

	bool checkPos () {
		bool result = true;

		if (forward && transform.position.x != toPos.x) {
			result = true;
		}
		else {
			forward = false;
		}

		if (!forward && transform.position.x != fromPos.x) {
			result = false;
		}
		else {
			forward = true;
		}

		return result;
	}

	void OnTriggerEnter2D(Collider2D other) {
		if (other.gameObject.CompareTag ("Player")) {
			touch = true;
			playerParent = other.transform.parent;
			other.transform.parent = transform;
		}
	}

	void OnTriggerExit2D(Collider2D other) {
		if (other.gameObject.CompareTag ("Player")) {
			touch = false;
			other.transform.parent = playerParent;
		}
	}
}