Search Unity

[SOLVED] Wan't to be able to both click my button and buttons that's behind it (Inventory)

Discussion in 'Getting Started' started by MCrafterzz, Jun 3, 2017.

  1. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    I have just started working on a rpg and have created a inventory. Important information: I have multiple slots that are buttons and I also have a item which is a button aswell. The item followed the mouse when selected.

    My problem is when holding the item I can't click on the slots because it clickes on the item. Both the item and the slots need to be buttons for it to work and because I have multiple slots and will have multiple items I can't do something specific for every gameobject. So as the title says I want to both click the item and the slot at the same time.

    Thanks for your help! Just ask me if you need more information.

    Also sorry if this is the wrong place as it is my first post
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    This is the right place, and welcome to the forums!

    If I understand you correctly, what you need to do is set raycastTarget = false on the Image component of the thing you're holding, while you're holding it. That will make clicks go right through it to whatever's behind. Then you can set it back to true once you've placed it down.
     
  3. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    Thanks, I have a place in the code where I could add this. The problem is that I haven't found a way to find the button component. Because that's where the variable is right? In code it gives a error when using GetComponent<Button>(): I am using csharp
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, I think you want the Image component, not the Button component.

    Also, as a general rule: if you're going to post about an error, actually copy and paste the error into your post. "An error" tells us nothing.
     
  5. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    Ok, GetComponent<Image> gave this error: CS0103: The name 'Image' does not exist in the current context.

    EDIT: I got it working by adding a canvasgroup which was acceseble, still can't find a way to access the image component which would be the prefered way. It does say that it is a script http://imgur.com/a/KTjVh
     
    Last edited: Jun 4, 2017
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    You're most likely missing the appropriate include. Place this at the top of the script with the others.

    Code (csharp):
    1. using UnityEngine.UI;
     
  7. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    Thanks both of you, it now works. A last question tho why haden't it inported UnityEngine.UI when it had inported UnityEngine, shouldn't that inport all UnityEngine classes?
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    Using has nothing to do with importing (in fact if the classes hadn't been imported the using directive would have generated an error message). It has to do with namespaces which are used to organize classes. By placing the using directive in your script you are informing the compiler that you wish to make use of classes found in that namespace.

    Without the using directive you would have had to specify the namespace when you referred to the class. Like below.

    Code (csharp):
    1. GetComponent<UnityEngine.UI.Image>();
    For more information or better examples I recommend checking out the official Unity tutorial for namespaces.

    https://unity3d.com/learn/tutorials/topics/scripting/namespaces?playlist=17117

     
    Last edited: Jun 4, 2017
    Kiwasi, Schneider21 and JoeStrout like this.
  9. MCrafterzz

    MCrafterzz

    Joined:
    Jun 3, 2017
    Posts:
    354
    Thanks!