Hi,
I'm running into a really weird problem with the new Anchoring System with trying to anchor to a game object in code.
What I'm attempting to do is to respond to a scoring event by instantiating a game object at the desired location and a UILabel which is then anchored to that game object in order to create a floating score display above that point on the play field.
The behavior that I'm getting that I find problematic is that, rather than the label appearing at the location of the game object, it appears in the center of the screen and then goes shooting off to bottom left at high speed.
When the event is triggered, I instantiate a marker game object and a label game object. I parent the Label game object to the NGUI Camera, and the marker keeps a reference to it.
Object _PopUpGuiObject = Instantiate(m_PopUpScoreGUIDisplayPrefab);
Transform _PopUpGuiTransform = _PopUpGuiObject as Transform;
_PopUpGuiTransform.parent = PopUpParentLocator.Instance.transform;
_PopUpGuiTransform.localPosition = Vector3.zero;
_PopUpGuiTransform.localScale = Vector3.one;
_PosMarker.PopUpScoreGUIDisplayTransform = _PopUpGuiTransform;
The script on the marker sets the text and color for the label and then sets itself as the anchor. It seems no matter whether I set anchor.relative or anchor.absolute or not set any offsets for the anchor at all I get the same behavior.
m_PopUpScoreLabel.leftAnchor.target = transform;
m_PopUpScoreLabel.rightAnchor.target = transform;
m_PopUpScoreLabel.topAnchor.target = transform;
m_PopUpScoreLabel.bottomAnchor.target = transform;
//m_PopUpScoreLabel.leftAnchor.relative = (m_PopUpScoreLabel.width /2) * -1 ;
//m_PopUpScoreLabel.bottomAnchor.relative = ( m_PopUpScoreLabel.height / 2 ) * -1 ;
//m_PopUpScoreLabel.rightAnchor.relative = (m_PopUpScoreLabel.width /2) ;
//m_PopUpScoreLabel.topAnchor.relative = ( m_PopUpScoreLabel.height / 2 );
m_PopUpScoreLabel.hideIfOffScreen = false;
m_PopUpScoreLabel.AssumeNaturalSize();
Can you tell me what I'm doing wrong? I can't seem to set any game object as an anchor in code without experiencing this same behavior.