Search Unity

The same function works horrible in one situation,works perfectly in update.

Discussion in 'Scripting' started by Reedex, May 21, 2019.

  1. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    well i have this function on my Enemy.cs, which gets called when it health drops below 0.
    Code (CSharp):
    1. void SpawnAfterDeath()
    2.     {
    3.         GameObject drop = (GameObject) Instantiate (InventoryManager.Instance.dropItem, transform.position, Quaternion.identity);
    4.         drop.AddComponent<ItemScript> ();
    5.         ItemScript itemScript = drop.GetComponent<ItemScript> ();
    6.         itemScript.Item = ItemGenerato.Instance.HandOutRandomItem(Mlvl,(int)TC);
    7.         drop.GetComponent<ItemProperties> ().name = itemScript.Item.ItemName;
    8.     }
    but i have 90% 15ms lag spike,
    but when i put in Player.cs Update on Input.GetKey (every frame)
    its, around 0.7% like nothing happens.

    from what i gather , the profiler says its this line
    Code (CSharp):
    1. itemScript.Item = ItemGenerato.Instance.HandOutRandomItem(Mlvl,(int)TC);
    and from there it's ItemScript.set_Item then its Resources.load, which i use only here
    (the profiler goes deeper into Loading.ReadObject, Loading.ReadObjectThreaded, and lastly
    Loading.LoadFileHeaders)
    here
    Code (CSharp):
    1. public class ItemScript : MonoBehaviour {
    2.  
    3.     public Sprite spriteNeutral;
    4.  
    5.     private Item item;
    6.     public Item Item {
    7.         get {    return item;    }
    8.         set {    item = value;
    9.  
    10.             spriteNeutral = Resources.Load<Sprite> (value.SpriteNeutral);   // this is itemscript.set_Item
    11.         }
    12.     }
    13.  
    14.     public string GetTooltip(Inventory inv)
    15.     {
    16.         return item.GetTooltip (inv);
    17.     }
    18.  
    19.     public void Use(Slot slot)
    20.     {
    21.         item.Use (slot,this);
    22.     }
    23. }
    it doesn't matter if i hit the enemy with arrow (trigger) or melee (raycast), results are the same.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    It's hard to tell without deep profile. But I would assume the resource is cached somewhere - hdd cache, os cache, or else, because accessed very often. And goes out of cache when used less oftn like on enemy death. You'd better load all resources prior running a game, or you'll experience a lot of unpredictable and hard-to-explain behaviours.
     
  3. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    yesterday i was told i should move the Item setter especially the sprite = resources.load line, onto anoher thread would you think it's difficult ? if not could you help me out a bit? :- )
    sorry to bother.
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    It's an overkill solution and may break your animation. Imagine sprite loading took 0.5 seconds, this means the sprite will appear 15 to 30 frames after mob actually drops an item. It's much easier to move all load calls into scene loading process and don't bother systems during the gameplay. After that, only if you'll find out the loading process is too long, then try threads or other complex solutions.
     
  5. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    what animation?, the way it works now its, I kill a mob,function SpawnAfterDeath (look above), instantiates bag on the ground ,adds the ItemScript.cs and sets the Item to one thats randomly picked. that's when the sprite loading is.
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I was thinking mod just drops an item(sprite) on the ground.
     
  7. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    no , it drops prefab bag with itemscript, and after picking it up, only place when you can see sprite of that item is inventory.
     
  8. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    How many enemies with that script do you have?
     
  9. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    it can be just one
     
  10. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Post a deep profile of the frame where this is all happening.

    I try and keep all my slow code (IE resources.load and alike) at level load (like @palex-nx said)
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Did you check how many times you're instantiating that thing? It might be that you have a bug that's causing it to get called more than once.
     
  12. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    389
    well i can see it on the ground and i pick it up and thats it,..so its once, i'm going to try to cache those sprites at the beggining..
    here down at the very bottom is my new logic for caching sprites,.do you think thats the right way to cache all sprites though? :- )
    https://forum.unity.com/threads/loa...gives-spike-alternatives.681568/#post-4564021