Author Topic: weird issue with color of UISprite via scripting  (Read 1850 times)

Newton

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 4
    • View Profile
weird issue with color of UISprite via scripting
« on: April 10, 2014, 09:19:07 AM »
Hi,

I'm truly baffled about this, and it probably something very basic I'm overlooking:

I did several sprites, and their color needs to be different based on different terms on the start play. They all use the same script. all also have UIButton.
The weird part is that while the color doesn't change (stays the basic pre-played color which is red), I debugged it and it states that the color is different!

I got this in the console: ( * = the variable part)
"Object No. *1* needs to be green"
"the color of *1* is *RGBA(0.000, 1.000, 0.000, 1.000)*"
 - object is STILL red

and same with others.
doing color change while in play mode via key pressing works fine.

this is the relevant code:
UISprite testSP;

<in Start>
testSP = GetComponent<UISprite>();

<in function>
testSP.color = Color.green;
Debug.Log("the color of " + this.name + " is " + this.GetComponent<UISprite>().color);

What am I missing\doing wrong?
Thanks for any help!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: weird issue with color of UISprite via scripting
« Reply #1 on: April 11, 2014, 06:50:41 AM »
Did you put two widgets on the same object, or widget on the same object as a panel?

Newton

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: weird issue with color of UISprite via scripting
« Reply #2 on: April 11, 2014, 02:28:41 PM »
No to the first, and I'm not sure about the second:
the sprites are children of a scroll view, and the scroll view is child of a panel. this panel has UIWidget Container (removing it doesn't make any change)

neither the sprites or scroll view have another Widget...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: weird issue with color of UISprite via scripting
« Reply #3 on: April 12, 2014, 04:29:38 AM »
I can see something like this happening after you've moved a widget and failed to notify it that its parent has changed. Make sure to always instantiate UI components via NGUITools.AddChild(parent, prefab) and never via Instantiate. Also make sure that if you re-parent anything UI-related, call NGUITools.MarkParentAsChanged on it.

Newton

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: weird issue with color of UISprite via scripting
« Reply #4 on: April 12, 2014, 10:54:32 AM »
I used you general advise (because my objects were created in editor, not in run time), and re-created the whole panel and sprites with emphasis on creating child by child pre-planned, rather then create object and then drag it to be a child.

And now it's working!!! (although it's something I didn't want to do all over again, but at least it's working :) )
Thank you!