I was trying to change the color of cube like this.
using Interfaces;
using UnityEngine;
namespace Commands
{
public class ClickCommand : ICommand
{
private Color _cubeColor;
private readonly Color _color;
public ClickCommand(GameObject cube, Color color)
{
_cubeColor = cube.GetComponent<MeshRenderer>().material.color;
_color = color;
}
public void Execute()
{
_cubeColor = _color;
}
public void Undo()
{
}
}
}
But it didn’t work. Then i tried this code.
using Interfaces;
using UnityEngine;
namespace Commands
{
public class ClickCommand : ICommand
{
private readonly Material _cubeMaterial;
private readonly Color _color;
public ClickCommand(GameObject cube, Color color)
{
_cubeMaterial = cube.GetComponent<MeshRenderer>().material;
_color = color;
}
public void Execute()
{
_cubeMaterial.color = _color;
}
public void Undo()
{
}
}
}
And it worked. Why the first code didn’t work? I didn’t change anything except this code