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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Image is stretching to canvas.

Discussion in 'Scripting' started by tiermann, Mar 17, 2015.

  1. tiermann

    tiermann

    Joined:
    Mar 15, 2015
    Posts:
    3
    Here is the code I'm using to try and put a green square on the center of the screen.
    What happens is the green fills the entire screen and I can't figure out why. It's probably something very simple that I'm missing, but I'm new to Unity.

    I've tried with a regular Image instead of RawImage, and setting the parent to the GameObject or Canvas doesn't make a difference.
    Code (CSharp):
    1. GameObject mapObject = new GameObject();
    2.  
    3. Canvas newCan = mapObject.AddComponent<Canvas>();
    4.         newCan.renderMode = RenderMode.ScreenSpaceOverlay;
    5.         newCan.transform.SetParent(mapObject.transform, false);
    6.  
    7. RawImage rImg = mapObject.AddComponent<RawImage>();
    8.      rImg.color = new Color(0f,1f,0f);
    9.      rImg.transform.SetParent(newCan.transform);
    10.      rImg.rectTransform.rect.Set((Screen.width/2)-8, (Screen.height/2)-8, 16, 16);
    11.  
    12. mapObject.SetActive(true);
     
    Last edited: Mar 17, 2015
  2. SeasiaInfotechind

    SeasiaInfotechind

    Joined:
    Nov 17, 2014
    Posts:
    32