Hello, I’m making a game with moving platforms that the player turns on and off. When the moving platforms turn off, they deactivate and the platforms freeze (probably a better way to do that but it’s for a school project due soon so it doesn’t need to be perfect). I want the moving platforms to have a shadow that stops when the original platform is inactive, and the code looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StopMovingShadows : MonoBehaviour
{
// counterpart is the real moving platform
public GameObject counterpart;
private Rigidbody2D rb;
void Start()
{
}
void Update()
{
if (counterpart.activeSelf == false)
{
rb.constraints = RigidbodyConstraints2D.FreezePosition;
}
else if (counterpart.activeSelf == true)
{
rb.constraints = RigidbodyConstraints2D.None;
}
}
}
When I hit play, I’m spammed with errors saying “Object reference not set to an instance of an object” directed at line 24:
rb.constraints = RigidbodyConstraints2D.None;
Does anyone know what’s happening?