Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[C#] Why isn't this working?

Discussion in 'Scripting' started by ImZeph, Aug 31, 2015.

  1. ImZeph

    ImZeph

    Joined:
    Jun 13, 2015
    Posts:
    19
    Here is my Slot script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Slot : MonoBehaviour {
    6.  
    7.     public Inventory Inventory;
    8.     public int CurrentItemID;
    9.  
    10.     public Sprite EmptySlot;
    11.     public Sprite SelectedEmptySlot;
    12.  
    13.     void Update() {
    14.         CurrentItemID = System.Array.IndexOf(Inventory.Sprites, this.transform.GetComponent<Image>().sprite);
    15.     }
    16.  
    17.     public void Select() {
    18.         this.transform.GetComponent<Image>().sprite = SelectedEmptySlot;
    19.     }
    20.  
    21.     public void Unselect() {
    22.         this.transform.GetComponent<Image>().sprite = EmptySlot;
    23.     }
    24. }
    25.  
    It is put on a UI Image. That Image has an Event Trigger. I have put a picture of it in the attached files. Why isn't this working? I'm trying to change the Images sprite when a mouse is over it. The function works when I put a Debug.Log in it but not when changing the sprite. What am I doing wrong?
     

    Attached Files:

  2. georetro

    georetro

    Joined:
    Jan 18, 2013
    Posts:
    218
    Could we see the actual event script? Also, I would recommend renaming one of your variables from:

    Code (csharp):
    1. public Inventory Inventory;
    To:

    Code (csharp):
    1. public Inventory inv;
     
  3. ImZeph

    ImZeph

    Joined:
    Jun 13, 2015
    Posts:
    19
    The Event Trigger script is not made by me. It comes by default in Unity. Everything works when I do it with something like a Debug.Log. It can't be a problem in the Event Trigger.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Try this and see what happens...

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class Slot : MonoBehaviour
    6. {
    7.     //public Inventory Inv;
    8.     //public int CurrentItemID;
    9.  
    10.     public Sprite EmptySlot;
    11.     public Sprite SelectedEmptySlot;
    12.     private Image _img;
    13.  
    14.     void Start()
    15.     {
    16.         _img = GetComponent<Image>();
    17.         Debug.Log(_img);
    18.     }
    19.     void Update()
    20.     {
    21.         //CurrentItemID = System.Array.IndexOf(Inv.Sprites, GetComponent<Image>().sprite);
    22.     }
    23.  
    24.     public void Select()
    25.     {
    26.         Debug.Log("Select Event Fired.");
    27.         _img.sprite = SelectedEmptySlot;
    28.     }
    29.  
    30.     public void Unselect()
    31.     {
    32.         Debug.Log("Unselect Event Fired.");
    33.         _img.sprite = EmptySlot;
    34.     }
    35. }
     
  5. ImZeph

    ImZeph

    Joined:
    Jun 13, 2015
    Posts:
    19
    It gives me the Debug.Logs but the sprite isn't changed.
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    was _img null?

    Tested and worked fine for me. Are you sure you don't have the same image in each slot? Maybe they aren't Sprites (import settings)? Image component isn't on the same GameObject?
     
    Last edited: Aug 31, 2015
  7. ImZeph

    ImZeph

    Joined:
    Jun 13, 2015
    Posts:
    19
    I don't know what's happening.


    Here's my code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Slot : MonoBehaviour
    6. {
    7.     //public Inventory Inv;
    8.     //public int CurrentItemID;
    9.  
    10.     public Sprite EmptySlot;
    11.     public Sprite SelectedEmptySlot;
    12.     public Image _img;
    13.  
    14.     void Start()
    15.     {
    16.         _img = GetComponent<Image>();
    17.         Debug.Log(_img);
    18.     }
    19.     void Update()
    20.     {
    21.         //CurrentItemID = System.Array.IndexOf(Inv.Sprites, GetComponent<Image>().sprite);
    22.     }
    23.  
    24.     public void Select()
    25.     {
    26.         Debug.Log("Select Event Fired.");
    27.         _img.sprite = SelectedEmptySlot;
    28.     }
    29.  
    30.     public void Unselect()
    31.     {
    32.         Debug.Log("Unselect Event Fired.");
    33.         _img.sprite = EmptySlot;
    34.     }
    35. }
    P.S I made the _img public to see if it's null when the game launches.
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Thats odd, works fine for me. What version of Unity are you on? Looks like an older one based on the icons. Perhaps there is a bug or something in the older versions... Seems like a rather big bug to still exist unnoticed, though.

    Not sure what else to suggest, it works in my tests with the same settings in 5.2...
     
  9. ImZeph

    ImZeph

    Joined:
    Jun 13, 2015
    Posts:
    19
    I'm on 5.1.
     
  10. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    I would test but I don't have it installed on this machine.

    Have you tried a fresh blank scene/project? Maybe some other script is interfering.
     
  11. ImZeph

    ImZeph

    Joined:
    Jun 13, 2015
    Posts:
    19
    I will try now.
     
  12. ImZeph

    ImZeph

    Joined:
    Jun 13, 2015
    Posts:
    19
    It works in a new project. I have no idea what it could be. There are no other scripts on the Slot 1 object itself.
     
  13. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    You probably have some other script changing the sprite... One of the reasons I commented out the code in Update(). Check your other inventory scripts, or just try creating a new object to narrow down if its just something with a reference to that icon or something finding all of those icons.
     
  14. ImZeph

    ImZeph

    Joined:
    Jun 13, 2015
    Posts:
    19
    ahahahahhahaha I opened up the inventory script and the first thing that shows up is a line of code that changes the sprite. Thank's man. You saved me. BTW Deftly looks really cool. ;)
     
    LaneFox likes this.