Author Topic: How to instantiate a Position-Tweened-label prefab at desired position?  (Read 2691 times)

alexh0101

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 30
    • View Profile
I have a prefab that has a child. This child has UILabel, Tween Position and Tween Alpha attached to it.

I set it to move from (0,0,0) to (0,2,0). These are the local position of the label.

When I drag and drop and instance of that prefab in the scene and hit play, it works as expected.
But when my game uses the method Instantiate(), the spawned labels appear at GLOBAL position (0,0,0).

In this case, am I supposed to use TweenPosition.Begin() as suggested in another thread?
I tried using it but I couldn't get it to work.

Thanks in advance



ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to instantiate a Position-Tweened-label prefab at desired position?
« Reply #1 on: September 01, 2014, 02:40:48 PM »
First, never use Instantiate. Use NGUITools.AddChild.

And yes, ideally use TweenPosition.Begin to start your tween.

alexh0101

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: How to instantiate a Position-Tweened-label prefab at desired position?
« Reply #2 on: September 01, 2014, 02:54:29 PM »
Hi there, thanks for your response.

I actually want to spawn that label right when the parent object is destroyed (I'm doing a scoring label like "+10").

Adding a child to that gameObject wouldn't work in that case since the gameObject gets destroyed.
Is there an alternative, so that I can spawn that prefab exactly where that object was destroyed?


Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How to instantiate a Position-Tweened-label prefab at desired position?
« Reply #3 on: September 01, 2014, 05:30:43 PM »
Just forward the global position of the original object to whatever spawns the label, and set its .position to that.

alexh0101

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: How to instantiate a Position-Tweened-label prefab at desired position?
« Reply #4 on: September 01, 2014, 07:28:47 PM »
But the problem is that the coordinates of the original object are something like (0,-5,0) but the coordinates of my tween label should be approx (0,-2000,0) in order to appear at the same place as the (0,-5,0).

I'm getting mixed up with screen coordinates and world coordinates.
Do I have to convert world coordinates to screen coordinates and set the "from" and "to" via scripting?

Thanks a lot
« Last Edit: September 01, 2014, 07:49:04 PM by alexh0101 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to instantiate a Position-Tweened-label prefab at desired position?
« Reply #5 on: September 02, 2014, 11:43:22 AM »
You are confusing local and world coordinates.

transform.position = world
transform.localPosition = local

Look in NGUIMath. It adds extension methods to transform -- OverlayPosition. It does all the necessary math inside.