So as we all know the UI Image has more components, but I need to acces the color of the Image and the Source Image from script so that i can change them . I tried with this script but it's not working. Code (JavaScript): var button = transform.Find("Button"); var buttonImage = background.GetComponent(UnityEngine.UI.Image); print(buttonImage.color); buttonImage.color = Color.red;
Here's how I'm doing it: Code (csharp): void Update() { Vector2 localPoint = Vector2.zero; if (GameManager.riftMode) { screenPoint = cam.WorldToScreenPoint(vrCursor.transform.position); } else { screenPoint = Input.mousePosition; } RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPoint, cam, out localPoint); rectTransform.GetLocalCorners(localCorners); imgWidth = localCorners[3].x - localCorners[0].x; imgHeight = localCorners[1].y - localCorners[0].y; Vector2 positionNormalizedForTexCoords; positionNormalizedForTexCoords.x = localPoint.x / imgWidth + 0.5f; positionNormalizedForTexCoords.y = localPoint.y / imgHeight + 0.5f; if (Input.GetMouseButton(0)) { if (positionNormalizedForTexCoords.x >= 0 && positionNormalizedForTexCoords.x <= 1 && positionNormalizedForTexCoords.y >= 0 && positionNormalizedForTexCoords.y <= 1) { pickedColor = tex.GetPixelBilinear(positionNormalizedForTexCoords.x, positionNormalizedForTexCoords.y); //print(positionNormalizedForTexCoords + " pickedColor " + pickedColor); UpdatePickedColorGameObject(pickedColor); } } } void UpdatePickedColorGameObject(Color color) { Image img = pickedColorGameObject.GetComponent<Image>(); img.color = color; }