Search Unity

Need help with creating color options

Discussion in 'Getting Started' started by Iqew, Oct 21, 2017.

  1. Iqew

    Iqew

    Joined:
    Nov 26, 2016
    Posts:
    38
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Colors : MonoBehaviour {
    6.  
    7. //Little Explanation
    8. //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.
    9. //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.
    10.  
    11.     public Color[] ActiveColors = new Color[3];
    12. // The Active pallete which is where the objects will get their color values
    13.     public Gradient[] ActiveGradients = new Gradient[2];
    14. //The Active Gradients which is where the trails in the scene will get their respective colors
    15.    
    16.     public Color[] DefaultPallete = new Color[3];
    17.  // Represents the default pallete applied to ActiveColors
    18.     public Gradient[] DefaultGradient = new Gradient[2];
    19. // Represents the default Gradients applied to ActiveGradients
    20.  
    21.     public Color[] Pallete1 = new Color[3];
    22. // Starting from this pallete, all other palletes are the options for other colors.
    23.     public Gradient[] Gradient1 = new Gradient[2];
    24. //Starting from these Gradients, all other gradients are options
    25.  
    26.     public Color[] Pallete2 = new Color[3];
    27.     public Gradient[] Gradient2 = new Gradient[2];
    28.  
    29.     public Color[] Pallete3 = new Color[3];
    30.     public Gradient[] Gradient3 = new Gradient[2];
    31. }

    I then put some code in the Start() of each gameObject that needs color changing.
    Code (CSharp):
    1. sing System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.  
    8.     public GameObject DefeatParticles;
    9.     public float Speed = 2f;
    10.     public GameObject ColorHolder;
    11.     private float ScreenHalfSize;
    12.     private float PlayerHalfWidth;
    13.  
    14.     void Start () {
    15.         ScreenHalfSize = Camera.main.orthographicSize * Camera.main.aspect;
    16.         PlayerHalfWidth = transform.localScale.x / 2;
    17.         GetComponentInChildren<TrailRenderer>().colorGradient = ColorHolder.GetComponent<Colors>().ActiveGradients[0];
    18.         GetComponentInChildren<Renderer>().material.color = ColorHolder.GetComponent<Colors>().ActiveColors[0];
    19.     }
    20.  
    21.     void Update () {
    22.         float Xinput = Input.GetAxisRaw ("Horizontal");
    23.         transform.Translate (Xinput * Speed * Time.deltaTime, 0, 0);
    24.  
    25.         if (transform.position.x > ScreenHalfSize) {
    26.             transform.position = new Vector3 (-ScreenHalfSize + PlayerHalfWidth, 0, 0);
    27.         }
    28.         if (transform.position.x < -ScreenHalfSize) {
    29.             transform.position = new Vector3 (ScreenHalfSize - PlayerHalfWidth, 0, 0);
    30.         }
    31.     }
    32.  
    33.     void OnTriggerEnter(Collider other){
    34.         if(other.CompareTag("FallingBlock")){
    35.             Instantiate(DefeatParticles, transform.position, Quaternion.identity);
    36.             Destroy(gameObject);
    37.         }
    38.     }
    39. }

    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
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I'm assuming the color arrays are defined in the Inspector? I'm wondering if the fact that you're initializing them in the class is causing them to be zeroed out each time you run the game.

    Try removing the initialization part (everything after and including the equals sign in the first script) and ensure all your colors are still defined in the Inspector, then give it a shot.
     
  3. Iqew

    Iqew

    Joined:
    Nov 26, 2016
    Posts:
    38
    I did what you said but sadly it had no effect.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Colors : MonoBehaviour {
    6.  
    7.     public Color[] ActiveColors;
    8.     public Gradient[] ActiveGradients;
    9.    
    10.     public Color[] DefaultPallete;
    11.     public Gradient[] DefaultGradient;
    12.  
    13.     public Color[] Pallete1;
    14.     public Gradient[] Gradient1;
    15.  
    16.     public Color[] Pallete2;
    17.     public Gradient[] Gradient2;
    18.  
    19.     public Color[] Pallete3;
    20.     public Gradient[] Gradient3;
    21. }
    upload_2017-10-21_11-45-46.png