Author Topic: Instantiated Button/Sprite Issue  (Read 5624 times)

stevej

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 117
    • View Profile
Instantiated Button/Sprite Issue
« on: January 17, 2015, 01:13:00 AM »
See video below.

I have a button called "ChestEntry" that I instantiate into a UIGrid to show individual items within a "treasure chest".

Basically, I iterate through a list of items that should be in the chest and execute something like:

  1. GameObject newChestEntryGo = Instantiate(chestEntry, Vector3.zero, Quaternion.identity) as GameObject;
  2. newChestEntryGo.GetComponent<UIButton>().normalSprite = thisItem;
  3. newChestEntryGo.transform.parent = gridChestContentGo.transform;
  4.  

At the end, I tell the UIGrid to reposition and then it's all displayed on screen.

The issue seems to be that the sprite applied in code for each item doesn't seem to "stick" and it constantly changes back to the "empty" sprite that's assigned to the source GameObject (ChestEntry) that's copied for all the others. Hopefully the video makes the issue a little clearer than I can describe it :)

Any thoughts on whether or not this is something wrong with my approach, or is it an NGUI bug?

Thanks!

<video removed>
« Last Edit: January 17, 2015, 02:25:17 AM by stevej »

extremist07

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Instantiated Button/Sprite Issue
« Reply #1 on: January 17, 2015, 01:28:21 AM »
I'm not sure if this is the right solution, but ArenMook has clearly stated that we shouldn't use instantiate.

http://www.tasharen.com/forum/index.php?topic=11208.0

Try using the AddChild method, might work better

stevej

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Instantiated Button/Sprite Issue
« Reply #2 on: January 17, 2015, 01:33:41 AM »
Oooo! Hadn't seen that before. Have switched to:

  1. GameObject newChestEntryGo = NGUITools.AddChild(gridChestContentGo, chestEntry);
  2. newChestEntryGo.GetComponent<UIButton>().normalSprite = thisItem;
  3.  

Exact same result/problem - but thanks for the info. Didn't realise there was a nicer way.

stevej

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Instantiated Button/Sprite Issue
« Reply #3 on: January 17, 2015, 02:20:32 AM »
Got lucky with a search and found the following:

http://www.tasharen.com/forum/index.php?topic=11714.msg54402

Which fixes the problem for me.

Hoping that will be fixed in a future version.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Instantiated Button/Sprite Issue
« Reply #4 on: January 18, 2015, 04:26:52 AM »
That code is already there. Sounds like you didn't update your NGUI.