Rect.contains() with a list

Hi, I have a little question, i have a List which contains 30 Rects. These Rect are added with an OnGUI function :

part of OnGUI():

                 Rect r = new Rect(52 * i, 52 * j, 50, 50);
                GUI.Box(r, content);
                GUI.DrawTexture(r, img);
                rList.Add(r);
                }

but i’d like to chech if the mouse is on one of the rect. So i did this in my Update Function:

    void Update () {
        if (rList.Count > 0) {
         for (int k = 0; k < rList.Count; k++)
            {
              if (rList[k].Contains(Event.current.mousePosition)) {
                    Debug.Log("Mouse OVER");
                }
            }
      }
    }

But this give me a console error when i start my game :

Does anyone know what is the matter ?

Thank you, and sorry for my english !

PS:

Of course in my class I have List rList; and I create the List during the start() function:rList = new List();

That is a good question. I am not sure what is wrong.
I will tell you, though, that I think you should step away from OnGUI. That is old and you should use the new UI stuff. :slight_smile:

And if your goal is to find out if the mouse is inside a rect in the new UI stuff, it will be pretty easy.

I agree with the switch to the new ui system as well. But, that aside, when posting an error it’s best to give the full error and what line it points to, otherwise we’re just guessing. Luckily I know what this error is, just not which line.

Post your exact start function - I’m pretty sure the issue is there.

In my start function i just start my List:
rList = new List<Rect>();rList = new List();

I was looking to do it un UI but i wasn’t able to place my image…

I mean the full start function. I ask because this:

suggests to me that you may have misnamed the Start() function (not capitalizing it). If it’s not capitalized it won’t run, and that would lead to the error you’re seeing.

    void Start()
    {
        rList = new List<Rect>();
        content = new GUIContent();
 
    

    }

The error is on this line :

  if (rList[k].Contains(Event.current.mousePosition)) {

Can you even call ‘Event.current’ from Update()? I’m not sure…
Are these rects always in the same place? If you don’t go the new UI route (reconsider, really think about it :))
You can just add all of the rects on Start() .

Anyways, back to Event.current. You can use: Input.MousePosition instead of Event.current

1 Like

Ok but can you explain me how to do it ? Because i’ll looked for it for a week and couldn’t find a way to create 30 Rect and change their image…

include:

 using UnityEngine.UI;

make a sprite variable(s):

 public Sprite mySprite;

and access the Image component and set the sprite.

GetComponent<Image>().sprite = mySprite;

Something like that.

Yes but how to create images, set their position, their image in a scrollView, I had problem with the scrollview

I don’t really understand what you mean by create the image. Do you mean instantiate an Image UI element to add to a scroll view? And if the list is fixed (not changing), you can create it all in the inspector.
Position depends on how you want the scroll view setup.
“problem with scrollview” is too vague…

The list is not fixed. I’m trying to create an inventory system. I need to add 30 images (50x50) in a panel which is a ScrollRect. But I don’t manage to do that…

Yep yep. so that’s no problem. Maybe you want to add a GridLayout to your scroll view. Whenever you add images, you can use code very similar to what I wrote above. Is this making any sense?

I tried your solution,

c is a gameObject (a panel)

        Image i = c.AddComponent<Image>();
        Sprite mySprite = Sprite.Create(Resources.Load("hache") as Texture2D, new Rect(0.0f, 0.0f, 50, 50), new Vector2(0f, 0f), 0f);
        i.GetComponent<Image>().sprite = mySprite;
        i.GetComponent<RectTransform>().sizeDelta = new Vector2(50f, 50f);
        i.GetComponent<RectTransform>().position = new Vector2(0, 0);

But it doesn’t work, nothing appears and i have an error:

No need to add “Image i = c.AddComponent()” … no need for that part. A rect transform (panel) already has an image & that is what the error is telling you.
Erase that line & then try again

Oops. Maybe instead of erase I should have said change it to:

 Image i = c.GetComponent<Image>();

lol my bad :slight_smile:

Ok i created a GridLayout, but how to add images to it ?