My game is very simple avoid the falling blocks game and I wanted to create a system where the player can choose a from a set of palletes to change the colors of the objects in the game including their trails. To do this, I created an Empty GameObject and attached a script to it that contains the pallettes the in the form of arrays. ColorHolderScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Colors : MonoBehaviour {
//Little Explanation
//Basically a menu system will be implemented where the player can press a button to apply a certain theme or color palette to the game.
//Each button represents a particular palette and Gradient set and if pressed will set the value of ActiveColors and ActiveGradients to that particular palette or Gradient Set.
public Color[] ActiveColors = new Color[3];
// The Active pallete which is where the objects will get their color values
public Gradient[] ActiveGradients = new Gradient[2];
//The Active Gradients which is where the trails in the scene will get their respective colors
public Color[] DefaultPallete = new Color[3];
// Represents the default pallete applied to ActiveColors
public Gradient[] DefaultGradient = new Gradient[2];
// Represents the default Gradients applied to ActiveGradients
public Color[] Pallete1 = new Color[3];
// Starting from this pallete, all other palletes are the options for other colors.
public Gradient[] Gradient1 = new Gradient[2];
//Starting from these Gradients, all other gradients are options
public Color[] Pallete2 = new Color[3];
public Gradient[] Gradient2 = new Gradient[2];
public Color[] Pallete3 = new Color[3];
public Gradient[] Gradient3 = new Gradient[2];
}
I then put some code in the Start() of each gameObject that needs color changing.
LookAtTheStartFunction
sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public GameObject DefeatParticles;
public float Speed = 2f;
public GameObject ColorHolder;
private float ScreenHalfSize;
private float PlayerHalfWidth;
void Start () {
ScreenHalfSize = Camera.main.orthographicSize * Camera.main.aspect;
PlayerHalfWidth = transform.localScale.x / 2;
GetComponentInChildren<TrailRenderer>().colorGradient = ColorHolder.GetComponent<Colors>().ActiveGradients[0];
GetComponentInChildren<Renderer>().material.color = ColorHolder.GetComponent<Colors>().ActiveColors[0];
}
void Update () {
float Xinput = Input.GetAxisRaw ("Horizontal");
transform.Translate (Xinput * Speed * Time.deltaTime, 0, 0);
if (transform.position.x > ScreenHalfSize) {
transform.position = new Vector3 (-ScreenHalfSize + PlayerHalfWidth, 0, 0);
}
if (transform.position.x < -ScreenHalfSize) {
transform.position = new Vector3 (ScreenHalfSize - PlayerHalfWidth, 0, 0);
}
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("FallingBlock")){
Instantiate(DefeatParticles, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
However, whenever the game starts. All the objects just become black and the trails become grey.
What’s the problem here? I already set the values of each pallete and gradient in the inspector.
Please help