GUI texture that changes the color of my object when I pressed it

Hello everyone,

I have this code( C# ) that should allow me to press a texture, and it would change the color of my object. But it does not work … and I have no error message. Do you have any idee ?

using UnityEngine;
using System.Collections;

public class couleur : MonoBehaviour {
public Color shader;
public Texture texture;

void OnGUI ()	
	{
Rect rect = new Rect(10,10,100,100);
GUI.DrawTexture(rect,texture);
if(rect.Contains(Input.mousePosition)  (Input.GetMouseButtonDown(0)))
			
   {
      renderer.material.color = shader;		
   }	
	}
}

your problem is that mouse input has different starting coordinates than rects. so you will have to inverse the y coordinate of the mouse to calculate the rect.Contains condition.
so replace the first condition in your if with this code:

rect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))

see if it works.

thank you very much ! it works :slight_smile: