Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: damon00 on January 29, 2014, 02:03:59 AM

Title: Trying to change my cursor and confused about how to reference my Atlas
Post by: damon00 on January 29, 2014, 02:03:59 AM
Here's my code:

UICursor.Set(UIAtlas,"Book_00");

My atlas is called MainHUD, and I made it using the Atlas Maker, but I don't know how to reference it in UICursor.Set.

I've looked at every bit of example code I could find, including the InventorySystem example, and I still don't understand this one part.

If anyone could explain it to me I would greatly appreciate it!!!
Title: Re: Trying to change my cursor and confused about how to reference my Atlas
Post by: ArenMook on January 29, 2014, 09:32:13 AM
UIAtlas is a component. It is attached to a prefab. If you know the prefab, do a GetComponent<UIAtlas>() on it to retrieve the atlas. Same as any other Unity component.
Title: Re: Trying to change my cursor and confused about how to reference my Atlas
Post by: damon00 on January 29, 2014, 10:30:46 AM
Thanks ArenMook, I'll give that a try!
Title: Re: Trying to change my cursor and confused about how to reference my Atlas
Post by: damon00 on January 29, 2014, 11:27:46 AM
Ok, I think I'm getting close, but still missing something...

I'm now no longer getting any errors, but the cursor still doesn't change.

Here's my code now:
  1. public class UI_Player_Inventory_Slot : MonoBehaviour {
  2.  
  3.         public int slot;
  4.  
  5.         UIAtlas mAtlas;
  6.  
  7.  
  8.         // Use this for initialization
  9.         void Start () {
  10.                
  11.         }
  12.  
  13.         void Awake ()
  14.         {
  15.                 mAtlas = GetComponent<UIAtlas> ();
  16.         }
  17.        
  18.         // Update is called once per frame
  19.         void Update () {
  20.        
  21.         }
  22.  
  23.         void OnClick ()
  24.         {
  25.                 Debug.Log ("inventory slot clicked");
  26.                 Game_Manager.Player_Inventory_Slots [slot].item_id = 1;
  27.  
  28.                 if (Game_Manager.Player_Inventory_Slots [slot].item_id > 0)
  29.                 {
  30.                         //pickup item
  31.                         Game_Manager.held_item = Game_Manager.Player_Inventory_Slots [slot].item_id;
  32.                         Game_Manager.Player_Inventory_Slots [slot].item_id = 0;
  33.  
  34.                         UICursor.Set(mAtlas,"Book_00");
  35.                 }
  36.         }
  37. }
  38.  

Any ideas on what I'm missing?
Title: Re: Trying to change my cursor and confused about how to reference my Atlas
Post by: damon00 on January 29, 2014, 12:57:40 PM
BTW, I don't know if this is relevant or not, but I am currently using the default windows cursor in my project. In other words I haven't done anything with the cursor before this.
Title: Re: Trying to change my cursor and confused about how to reference my Atlas
Post by: ArenMook on January 29, 2014, 10:50:36 PM
Unless this script is attached to the same game object as your atlas, what you're doing is not going to work.

GetComponent is how you retrieve any MonoBehaviour-derived component in Unity. But this function should be called on the game object that actually has the component.

If you don't actually have the script attached to the same object, then you need to either reference the right object first, or simply make a public variable that you can drag & drop in inspector to the right value to begin with. I suggest you do just that -- make a "public UIAtlas myAtlas;" variable in your script, then drag & drop the value in inspector. No need to GetComponent anything.
Title: Re: Trying to change my cursor and confused about how to reference my Atlas
Post by: damon00 on January 30, 2014, 01:40:56 AM
I got it working, thanks!!  :D