Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change the color of a primitive (specifically a quad)?

Discussion in 'Scripting' started by tfishell, Nov 23, 2017.

  1. tfishell

    tfishell

    Joined:
    Nov 20, 2017
    Posts:
    97
    This seems like something that should be very simple. I'm following a script here (and the advice here) and can create the quad, but for some reason I can't seem to change its color. I've also tried setting quad's meshRenderer to a stored material (with a different color), but that doesn't seem to work either; I also tried SetColor. There must be something simple I'm missing.

    Code (CSharp):
    1. void Start(){
    2. //...
    3. GameObject boundingBoxQuad = GameObject.CreatePrimitive(PrimitiveType.Quad);
    4.         boundingBoxQuad.transform.localScale = new Vector3(viewWidth - spriteSize.x, viewHeight - spriteSize.y);
    5. //do I need to set a material first? boundingBoxQuad.GetComponent<Renderer>().material =?
    6.         boundingBoxQuad.GetComponent<Renderer>().material.color = Color.red;
    7.  
    8.     }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It seemed completely right to me, so I tested it just now to be sure. Works for me. Is that script on an active game object?
     
  3. tfishell

    tfishell

    Joined:
    Nov 20, 2017
    Posts:
    97
    Yeah. I tested a new script on a different object, and I still got just black quad. https://imgur.com/xYVjvup
    It's not a big deal obviously but man I wish I knew what the problem was.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class test : MonoBehaviour {
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         GameObject boundingBoxQuad = GameObject.CreatePrimitive(PrimitiveType.Quad);
    11.         boundingBoxQuad.transform.localScale = new Vector3(2, 3);
    12.         //Renderer boundingBoxRender = boundingBoxQuad.GetComponent<Renderer>();
    13.         boundingBoxQuad.GetComponent<Renderer>().material.color = Color.red;
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.        
    19.     }
    20. }
    21.  
    22.  
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just to double check something - if select that in the inspector, is the material black there, too?
    (I'm asking just to check that it's facing the right way, I guess? no insult intended:)).
     
  5. tfishell

    tfishell

    Joined:
    Nov 20, 2017
    Posts:
    97
    It's cool. No actually the material isn't black there. It's the Default-Material but red since I changed it. Mesh Renderer is set to Default Material too.

    If I fly around the quad in 3D view (vs the 2D view I'm normally in), it isn't visible from the other side.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay.. so it's facing the right way, and it's the correct colour in the inspector.

    In my test, I wasn't in 2d.. off the top of my head i'm not sure what's wrong. Maybe no light in the scene, is that it? Do you need a quad? In 2d, maybe a sprite is good? :)
     
  7. tfishell

    tfishell

    Joined:
    Nov 20, 2017
    Posts:
    97
    I just now tried putting in a directional light and that didn't seem to help. The quad is just being used to show a certain area while I practice, so it's not a big deal. But it's just frustrating to be unable to solve this. :p But again, not a big deal.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I hear ya. I love figuring things out (to work/why they're broken lol). But maybe just try a sprite.

    No idea what's wrong, sorry :)
     
  9. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    1. New scene with only camera in it. Position (0,0,-10)
    2. GameObject -> 3D Object -> Cube (you can also use a quad but a quad is not visible from all sides, so its orientation matters) --- in Inspector Position (0,0,0)
    3. Asset -> Create -> Material --- in project view change albedo (color) to red
    4. Drag Material from Project view onto cube in Hierarchy
    5. Now you should have a dark red cube in your game view
    6. The cube is affected by ambient light - this is due to it's material/shader. Check Window -> Lighting -> Settings -> Ambient Color. Change this and see how it affects cube
    7. If you want to get rid of lighting, go to cube in inspector and change the shader from standard to unlit/color
     
    tfishell likes this.
  10. tfishell

    tfishell

    Joined:
    Nov 20, 2017
    Posts:
    97
    Thanks, I'll try this out tomorrow.
     
  11. tfishell

    tfishell

    Joined:
    Nov 20, 2017
    Posts:
    97
    Just saying thanks again, this helped me out a bit.