This is a very strange problem i came across today.
Porting one of my IOS games to Android.
It works flawlessly in the Editor and for IOS, however building for Android device resulted in very strange behaviour!!!
The sprites that suppose to update their position according to where the player is and the target position, just don't update their position! first update cycle is done and then they just stop and sometimes disappear!
i did a debug print out to see if the position is changed when the player moves, and it updates once, then stops.
The editor is working just fine with no problem, also building for IOS has no problem. The sprits move and change their position every frame.
Basically this script is suppose to show an indication onscreen to where the next target is according the position of the player and the way he is facing.
If the indication sprite goes off screen, it keeps it inside the screen at the screen's corners.
using UnityEngine;
using System.Collections;
/// <summary>
/// Example script showing how to add UI window that follows an object drawn by another camera.
/// </summary>
[AddComponentMenu("NGUI/Examples/Unit HUD")]
public class UnitHUD : MonoBehaviour
{
/// <summary>
/// Target object this UI element should follow.
/// </summary>
public Transform target;
Transform mTrans;
Camera mGameCam;
Camera mUICam;
Vector3 mPos;
bool mVisible = true;
public float YAdjustFactor = 2f;
public Transform Player;
private UIWidget MyWidget;
public bool ChangeColor;
void Start ()
{
mTrans = transform;
//target = mTrans;
if (target == null) { Destroy(gameObject);
return; }
mGameCam = NGUITools.FindCameraForLayer(target.gameObject.layer);
mUICam = NGUITools.FindCameraForLayer(gameObject.layer);
MyWidget = gameObject.GetComponentInChildren<UIWidget>();
}
void LateUpdate()
{
if (target == null) { Destroy(gameObject); return; }
if (!target.gameObject.activeInHierarchy) {gameObject.SetActive(false); return;}
Vector3 screenpos = Camera.main.WorldToScreenPoint(target.position);
mPos = mGameCam.WorldToViewportPoint(target.position);
bool visible = (mPos.z > 0f && mPos.x > 0f && mPos.x < 1f && mPos.y > 0f && mPos.y < 1f);
if (visible){
mTrans.localPosition = screenpos;
MyWidget.enabled = false;
}
else {
if (mPos.z < 0)
mPos *= -1;
//print("mpos = "+mPos);
MyWidget.enabled = true;
mPos.x = Mathf.Clamp01(mPos.x);
mPos.y = Mathf.Clamp01(mPos.y);
//print("mpos Clamped = "+mPos);
if(mPos.x > 0.95f)
mPos.x = 0.95f;
if(mPos.y > 0.95f)
mPos.y = 0.95f;
if(mPos.x < 0.05f)
mPos.x = 0.05f;
if(mPos.y < 0.05f)
mPos.y = 0.05f;
mPos = mUICam.ViewportToWorldPoint(mPos);
mPos = MyWidget.transform.parent.InverseTransformPoint(mPos);
mPos.z = 0;
MyWidget.transform.localPosition = mPos;
}
}
}