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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Creating a 2d rectangle

Discussion in '2D' started by blingle, Apr 24, 2020.

  1. blingle

    blingle

    Joined:
    Apr 12, 2020
    Posts:
    17
    I'm trying to create a 2d rectangle in Unity that I want to be able to colour with solid random colours and change size/position with.
    I can easily change the size/positon, but I'm really struggling with the colour despite being conceptually really simple. How is this done?
    So far I've tried:
    * Sprite renderer - but the colour doesn't change and I don't have a sprite anyway, it's just a simple rectangle.
    * MeshRenderer with a quad mesh - but:
    ** Set the thickness to 0 and it turns solid black, so it has to at least 0.00001 thick; not sure why (?)
    ** The colour of the material has all of the fancy shading effects on,
    ** The Rectangle is always behind the "background" sprite; There doesn't seem to be a "sorting layer" on the MeshRenderer.
    Any suggestions?
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    Try the Image component, and change the colour on that to get it to change colours. If you're looking only for an outline, i've seen some shaders/scripts around that do that, can find them for you if needed.
     
  3. blingle

    blingle

    Joined:
    Apr 12, 2020
    Posts:
    17
    Thanks for the suggestion - note that isn't for the UI, so would a Image component be appropriate? It seems to be a UI thing. Doesn't seem to work without being on the Canvas.
     
  4. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    I think it should still be fine. You can also look at sprite renderer component it does the same thing but might be closer to what you want.
     
  5. blingle

    blingle

    Joined:
    Apr 12, 2020
    Posts:
    17
    I ended up just using a regular 3d cube but with a Z scale of 0.001. It seems it uses the Z axis for ordering over sprites.