So, I’m trying out mechanics to a game, and I thougt of a “Gravity changer” level, where you take an item and the gravity is inverted.
For that, I want the blocks/ground in the scene to change color when the gravity is inverted. For that, I created two tilesets (It’s a pixel art-esque game, so I’m creating empty objects as ground, where I add the collider, and add children to the ground object with just the Sprite renderer with the correct tile, this will be important later), one blue (for normal gravity, downGround) and one red (for inverted gravity, upGround). I also added a public boolean variable to my Character controller script to know when is the gravity inverted.
I thought about using the animator, but since I’m using two tilesets, it means I’d have to animate every single tile to change from one state to another, and I’m sure there must be a better way.
What I did was creating two ground objects (one with the downGround tileset and other one with the upGround tileset), and adding those as children into another ground object where I actually add the collider and the script that will manage the ground changing. At the beginning of the program, the upGround is disabled by default, so I figured out I needed to toggle the enable and disable of both grounds so they change (so, if gravity is inverted, downGround will be disabled instead and upGround enabled, and vice versa). For that I used this code:
public class GravityTileSwitch : MonoBehaviour
{
private PlayerController player;
public GameObject downGround, upGround;
void Start()
{
//Get PlayerController component public variables access
player = GetComponent<PlayerController>();
}
private void Update()
{
//Show upGround
if (player.invGravity)
{
downGround.SetActive(false);
upGround.SetActive(true);
}
//Show downGround
else if (!player.invGravity)
{
downGround.SetActive(true);
upGround.SetActive(false);
}
}
}
In the public GameObject variables I attach the corresponding object, downGround and upGround.
The problem is that, when I run the program, not only is the ground not changing, but the console displays an error that says
NullReferenceException: Object reference not set to an instance of an object GravityTileSwitch.Update () (at Assets/Scripts/GravityTileSwitch.cs:19)
Does somebody know why is my code not working? I’d appreciate a lot your help, thank you!