Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Unfixable serialization error - or no evidence that indicates the problem

Discussion in '2D Experimental Preview' started by Xelnath, Jun 22, 2016.

  1. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    Code (csharp):
    1.  
    2. get_gameObject is not allowed to be called during serialization, call it from Awake or Start instead. Called from MonoBehaviour 'FloatingDamageNumberManager' on game object 'DamageNumbers'.
    3. See "Script Serialization" page in the Unity Manual for further details.
    4. UnityEngine.UI.Graphic:OnRectTransformDimensionsChange()
    5.  
    Here's the code involved. Note that I did read the link about Serialization errors - but I have no idea from the above error *where* Im calling the unity API during serialization. :|

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. using System.Collections.Generic;
    6.  
    7. public static class FloatingTextColor
    8. {
    9.  public static Color PlayerDamage = new Color(0.9f, 185f/255f, 0f);
    10.  public static Color EnemyDamage = new Color(1.0f, 0.1f, 0.1f);
    11.  public static Color PlayerHealing = Color.green;
    12.  public static Color EnemyHealing = Color.Lerp(Color.red, Color.blue, 0.55f);
    13. }
    14. public class FloatingTextData : MonoBehaviour
    15. {
    16.  public Vector3 startPos = Vector3.zero;
    17.  public Vector3 endPos = Vector3.up;
    18.  
    19.  public bool isEnabled = false;
    20.  public float startTime = float.NegativeInfinity;
    21.  
    22.  public float duration = 2.0f;
    23.  
    24.  public bool isNumber = false;
    25.  
    26.  private Color _textColor = FloatingTextColor.PlayerDamage;
    27.  public Color TextColor
    28.  {
    29.  set
    30.  {
    31.  _textColor = value;
    32.  
    33.  }
    34.  }
    35.  
    36.  private string _format = "{0}";
    37.  public string Format
    38.  {
    39.  set
    40.  {
    41.  _format = value;
    42.  SetText();
    43.  }
    44.  get
    45.  {
    46.  return _format;
    47.  }
    48.  }
    49.  
    50.  private int _amount = 0;
    51.  public int Amount
    52.  {
    53.  set {
    54.  isNumber = true;
    55.  _amount = value;
    56.  SetText();
    57.  }
    58.  get
    59.  {
    60.  return _amount;
    61.  }
    62.  }
    63.  
    64.  public void SetText()
    65.  {
    66.  GetComponent<Text>().text = string.Format(_format, _amount);
    67.  }
    68.  
    69.  public void Reset()
    70.  {
    71.  _amount = 0;
    72.  _format = "{0}";
    73.  }
    74.  
    75.  public void Update()
    76.  {
    77.  if ( isEnabled )
    78.  {
    79.  if ( Time.time > startTime + duration )
    80.  {
    81.  isEnabled = false;
    82.  }
    83.  else
    84.  {
    85.  var progress = Mathf.Clamp01( (Time.time-startTime) / duration );
    86.  var evalMove = FloatingDamageNumberManager.instance.acMove.Evaluate( progress );
    87.  var evalAlpha = FloatingDamageNumberManager.instance.acAlpha.Evaluate( progress );
    88.  
    89.  var position = Vector3.Lerp (startPos, endPos, evalMove);
    90.  
    91.  //thisistheui element
    92.  RectTransform UI_Element = this.GetComponent<RectTransform>();
    93.  
    94.  //firstyouneedtheRectTransformcomponentofyour canvas
    95.  RectTransform CanvasRect = FloatingDamageNumberManager.instance.mainUICanvas.GetComponent<RectTransform>();
    96.  
    97.  //thenyoucalculatethepositionoftheUI element
    98.  //0,0forthecanvasisatthecenterofthescreen,whereasWorldToViewPortPointtreatsthelowerleftcorneras0,0.Becauseofthis,youneedtosubtracttheheight/widthofthecanvas*0.5togetthecorrectposition.
    99.  
    100.  Vector2 ViewportPosition=FloatingDamageNumberManager.instance.mainCamera.WorldToViewportPoint(position);
    101.  Vector2 WorldObject_ScreenPosition=new Vector2(
    102.  ((ViewportPosition.x*CanvasRect.sizeDelta.x)-(CanvasRect.sizeDelta.x*0.5f)),
    103.  ((ViewportPosition.y*CanvasRect.sizeDelta.y)-(CanvasRect.sizeDelta.y*0.5f)));
    104.  
    105.  //nowyoucansetthepositionoftheui element
    106.  UI_Element.anchoredPosition=WorldObject_ScreenPosition;
    107.  
    108.  var text = GetComponent<Text>();
    109.  //getthecurrentcolorofthe text
    110.  Color color = _textColor;
    111.  //setthealphavalueforfade out
    112.  color.a = evalAlpha;
    113.  //settextmeshcolortonew color
    114.  text.color = color;
    115.  }
    116.  }
    117.  if ( !isEnabled )
    118.  {
    119.  Reset();
    120.  gameObject.SetActive(false);
    121.  }
    122.  }
    123. }
    124.  
    125. public class FloatingDamageNumberManager : MonoBehaviour {
    126.  
    127.  public AnimationCurve acMove;
    128.  public AnimationCurve acAlpha;
    129.  
    130.  public GameObject FloatCombatTextPrefab;
    131.  
    132.  public Canvas mainUICanvas;
    133.  public Camera mainCamera;
    134.  
    135.  private ObjectPool _cachedPool;
    136.  private ObjectPool _damageNumbers
    137.  {
    138.  get
    139.  {
    140.  return _cachedPool ?? _initPool();
    141.  }
    142.  }
    143.  private ObjectPool _initPool()
    144.  {
    145.  
    146.  if ( _cachedPool == null )
    147.  _cachedPool = new ObjectPool( 10, "[Generated]DamageNumbers",
    148.  () =>
    149.  {
    150.  var rectParent = GameObject.Find("CombatUI").GetComponent<RectTransform>();
    151.  var go = GameObject.Instantiate(FloatCombatTextPrefab );
    152.  go.transform.SetParent( rectParent, false );
    153.  go.GetOrAddComponent<FloatingTextData>();
    154.  
    155.  return go;
    156.  } );
    157.  
    158.  return _cachedPool;
    159.  }
    160.  
    161.  private static FloatingDamageNumberManager _instance;
    162.  public static FloatingDamageNumberManager instance
    163.  {
    164.  get {
    165.  if ( _instance == null )
    166.  _instance = new FloatingDamageNumberManager();
    167.  
    168.  return _instance;
    169.  }
    170.  }
    171.  
    172.  public void OnEnable()
    173.  {
    174.  _instance = this;
    175.  
    176.  }
    177.  public void Update()
    178.  {
    179.  if ( mainCamera == null )
    180.  mainCamera = GameObject.FindGameObjectWithTag( "MainCamera" ).GetComponent<Camera>();
    181.  
    182.  if ( mainUICanvas == null )
    183.  {
    184.  var uiContainer = transform.parent.gameObject;
    185.  mainUICanvas = uiContainer.GetOrAddComponent<Canvas>();
    186.  
    187.  //Initializethepoolifnotalready done
    188.  _initPool();
    189.  var numbersContainer = GameObject.Find( "[Generated]DamageNumbers" );
    190.  numbersContainer.transform.SetParent( mainUICanvas.transform, false );
    191.  
    192.  }
    193.  }
    194.  
    195.  public void ResetPool()
    196.  {
    197.  if( _cachedPool != null )
    198.  {
    199.  foreach ( var go in _cachedPool.Pool )
    200.  {
    201.  go.SetActive ( false );
    202.  }
    203.  }
    204.  }
    205.  
    206.  public static float TextDuration = 2.0f;
    207.  public static float xOffset = 1.0f;
    208.  public static float FloatHeight = 1.5f;
    209.  
    210.  
    211.  public static Dictionary<int, GameObject> damageNumbers = new Dictionary<int, GameObject>();
    212.  
    213.  public static FloatingTextData GetDamageNumber( IUnit unit, Vector3 startPos )
    214.  {
    215.  //Firstcheckifthere'sarecentlycacheddamage number
    216.  
    217.  GameObject tDamageNumberGameObject;
    218.  
    219.  if ( damageNumbers.TryGetValue( unit.UnitID, out tDamageNumberGameObject ) )
    220.  {
    221.  FloatingTextData ftd = tDamageNumberGameObject.GetComponent<FloatingTextData>();
    222.  
    223.  if ( ftd != null )
    224.  {
    225.  //Ifarecentdata
    226.  if ( ftd.isEnabled && Time.time < ftd.startTime + (ftd.duration * 0.5f) )
    227.  return ftd;
    228.  }
    229.  }
    230.  
    231.  var goNewDamageNumber = FloatingDamageNumberManager.instance._damageNumbers.Request();
    232.  goNewDamageNumber.transform.position = startPos;
    233.  var fData = goNewDamageNumber.GetOrAddComponent<FloatingTextData>();
    234.  fData.Reset();
    235.  fData.startTime = Time.time;
    236.  fData.startPos = startPos;
    237.  
    238.  //ThisneedstobeconvertedintoapositionbasedonUIcanvas,notworld position
    239.  fData.endPos = startPos + new Vector3( Random.Range( -xOffset, xOffset ), FloatHeight, 0f );
    240.  fData.duration = TextDuration;
    241.  fData.isEnabled = true;
    242.  
    243.  damageNumbers[unit.UnitID] = goNewDamageNumber;
    244.  
    245.  return fData;
    246.  }
    247.  
    248.  
    249.  public static Dictionary<int, GameObject> healingNumbers = new Dictionary<int, GameObject>();
    250.  public static FloatingTextData GetHealingNumber( IUnit unit, Vector3 startPos )
    251.  {
    252.  //Firstcheckifthere'sarecentlycacheddamage number
    253.  
    254.  GameObject tDamageNumberGameObject;
    255.  
    256.  if ( healingNumbers.TryGetValue( unit.UnitID, out tDamageNumberGameObject ) )
    257.  {
    258.  FloatingTextData ftd = tDamageNumberGameObject.GetComponent<FloatingTextData>();
    259.  
    260.  if ( ftd != null )
    261.  {
    262.  //Ifarecentdata
    263.  if ( ftd.isEnabled && Time.time < ftd.startTime + (ftd.duration * 0.5f) )
    264.  return ftd;
    265.  }
    266.  }
    267.  
    268.  var goNewDamageNumber = FloatingDamageNumberManager.instance._damageNumbers.Request();
    269.  goNewDamageNumber.transform.position = startPos;
    270.  var fData = goNewDamageNumber.GetOrAddComponent<FloatingTextData>();
    271.  fData.Reset();
    272.  fData.startTime = Time.time;
    273.  fData.startPos = startPos;
    274.  
    275.  //ThisneedstobeconvertedintoapositionbasedonUIcanvas,notworld position
    276.  fData.endPos = startPos + new Vector3( Random.Range( -xOffset, xOffset ), FloatHeight, 0f );
    277.  fData.duration = TextDuration;
    278.  fData.isEnabled = true;
    279.  
    280.  healingNumbers[unit.UnitID] = goNewDamageNumber;
    281.  
    282.  return fData;
    283.  }
    284. }
    285.