Author Topic: How do I set UI2DSprite at runtime in Javascript?  (Read 2482 times)

mtran003

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
How do I set UI2DSprite at runtime in Javascript?
« on: September 18, 2014, 10:12:36 PM »
For the life of me, I've search and search but can't seem to find a way to get past this.  In my Javascript code, I have this:

myGameObject.GetComponent("UI2DSprite").sprite2D = "MySprite";

I get this error:

BCE0019: 'sprite2D' is not a member of 'UnityEngine.Component'.


My Kingdom on how to properly set UI2DSprite sprite name using Javascript during runtime.

Thanks in advance.
Manny

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How do I set UI2DSprite at runtime in Javascript?
« Reply #1 on: September 19, 2014, 08:26:30 PM »
sprite2D is of type UnityEngine.Sprite, yet you are trying to assign it by text.

You have two options: either pass a UnityEngine.Sprite instead, or change your UI2DSprite to be UISprite instead, using NGUI's atlases. Going with NGUI's atlases you will be able to assign sprites by name.