I have a ‘Change Color’ script that changes the current color of the attached game object when the middle mouse button is scrolled.
This script is attached to a FPS rifle. After changing the rifle’s color, it fires a splatter prefab that needs to be the current color of the rifle. The splatter shoots away from the rifle’s ‘InstantiatePoint’ and rapidly cycles through different colors. When the splatter hits the ground or a wall, it is still its original color.
I need the splatter to change to the rifle’s current color when the rifle is fired, and remain that color.
Here is the Color Changer script:
using UnityEngine;
public class ColorChanger : MonoBehaviour
{
// The Array of Colors to cycle through
public Color[ ] colors = new Color[ ] { Color.red, Color.green, Color.blue };
private int currentIndex = 0; // Current color index
void Update()
{
// Checks for Mouse Wheel Input
float scroll = Input.GetAxis(“Mouse ScrollWheel”);
if (scroll != 0)
{
UpdateColorIndex(scroll);
UpdateObjectColor();
}
}
// Updates the Color Index, based on the scroll direction
void UpdateColorIndex(float scrollDirection)
{
if (scrollDirection > 0) // Scroll up
{
currentIndex = (currentIndex + 1) % colors.Length;
}
else if (scrollDirection < 0) // Scroll down
{
if (currentIndex == 0)
currentIndex = colors.Length - 1;
else
currentIndex–;
}
}
// Applies the selected color to the object’s Material
void UpdateObjectColor()
{
Renderer renderer = GetComponent();
if (renderer != null)
{
renderer.material.color = colors[currentIndex];
}
}
}