Search code examples
c#imageunity-game-enginescriptinggameobject

Add Image component to new gameobject


I created a new game object:

GameObject newObject = new GameObject("ObjectName");
newObject.AddComponent<RectTransform>();
newObject.GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);

I'm looking for a way to add an image (script) for color purposes. How can I do it?


Solution

  • The same way you are adding RectTransform.

    newObject.AddComponent<Image>();
    

    You also need to add using UnityEngine.UI to be able to find the Image component.