Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can't change car color

Discussion in 'Scripting' started by 2k-sp, Jun 26, 2014.

  1. 2k-sp

    2k-sp

    Joined:
    Jun 26, 2014
    Posts:
    10
    Hi guys
    Actually i´m working on a project (realtime car)
    and i´v exported my car from blender to Unity . The problem i met is, it´s not possible
    to change the color using C# (Unity does not recongnize my object) with this simple code :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System.Collections;
    6.  
    7. public class NewBehaviourScript : MonoBehaviour {
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update ()
    16.  
    17.  
    18.     {
    19.         if (Input.GetKeyDown (KeyCode.G))
    20.         {
    21.             gameObject.renderer.material.color = Color.green;
    22.        }
    23.     }
    24. }
    25.  
     

    Attached Files:

    Last edited by a moderator: Jun 26, 2014
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Select the gameObject that actually has the renderer component on it.
    Next time be more specific with your thread title, and please exclude useless polls.
    Thanks in Advance c:
     
  3. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    I've edited this thread to add code tags, remove the pointless poll, and apply a better title. Please make better posts in the future.
     
  4. 2k-sp

    2k-sp

    Joined:
    Jun 26, 2014
    Posts:
    10
    sorry it´s my first time to use that forum
     
  5. 2k-sp

    2k-sp

    Joined:
    Jun 26, 2014
    Posts:
    10
    It´s my first time to use Untiy and i´m still learning how to programm could u help me more?
    And sorry it´s also my first time to use that forum
     
  6. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    The code you posted will only affect the renderer of the gameObject that the script is located on, and from your screenshot of the hierarchy, it's impossible to see if this is the case.
     
    Last edited: Jun 26, 2014
  7. 2k-sp

    2k-sp

    Joined:
    Jun 26, 2014
    Posts:
    10

    Alright . But it does not work when it comes for rendering
     
  8. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, what we are talking about is that a game object has a renderer (or doesnt have a renderer). So if you had a cube, and you did this:

    Code (csharp):
    1.  
    2. renderer.material.color = Color.red;
    3.  
    The cube would turn red. If you had multiple cubes parented to that cube, only the one on the lowest level would turn. (also, if it were parented, it would only turn red, and not the ones below it)

    Also, if you had an empty game object (or one without a renderer, like a camera) it would just give you an error, since renderer does not exist.

    Now, what you need to do, is to search through your hierarchy and change all the ones that you need changed. Consider:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour {
    7.     Color[] colors = new Color[]{Color.green, Color.red, Color.blue, Color.white, Color.black};
    8.     int index = 0;
    9.  
    10.     void Update(){
    11.         if (Input.GetKeyDown (KeyCode.G)){
    12.             SetColor(colors[index++]);
    13.             index = index % colors.Length;
    14.         }
    15.     }
    16.  
    17.     void SetColor(Color color){
    18.         Renderer[] rends = (Renderer[])transform.root.gameObject.GetComponentsInChildren<Renderer> ();
    19.         foreach (Renderer rend in rends) {
    20.             foreach(Material mat in renderer.materials){
    21.                 mat.color = color;
    22.             }
    23.         }
    24.     }
    25. }
    26.  
    See how I go through each object in the root, and find all of the renderers, then go through each material of each renderer and set all the colors to the new color.

    I also added a little cycle in there so that every time you press the G key, it changes color.
     
    2k-sp likes this.
  9. 2k-sp

    2k-sp

    Joined:
    Jun 26, 2014
    Posts:
    10

    i´m so thankful . It were exactly my problem . I tried to add an empty object game and i´ve got that error .
    thx ;)