Search Unity

Resources.Load Prefab help

Discussion in 'Scripting' started by frankxp70, Aug 16, 2019.

  1. frankxp70

    frankxp70

    Joined:
    Jul 7, 2019
    Posts:
    4
    Untitled.png Hi there!
    Still relatively new to Unity, so sorry if this is just some dumb rookie mistake.
    I have been following a youtube tutorial called "Damage Popup Text in Unity 5". However, I get stuck on using Resouces.Load to load a prefab. I have read all the common errors of not having a Resources folder etc. However, I still seem to be stuck and just can't load the Prefab. Any help and suggestions would be much appreciated!
     
    Last edited: Aug 17, 2019
  2. IcePhoenix_0101

    IcePhoenix_0101

    Joined:
    Sep 9, 2017
    Posts:
    32
    I think the Prefab PopupDamagePArent is a GameObject and you are trying to load it as a c# script.
    Try:
    Code (CSharp):
    1. DamageText = Resources.Load<GameObject>("Prefabs/PopupDamageParent").GetComponent<DamageAnim>();
     
  3. frankxp70

    frankxp70

    Joined:
    Jul 7, 2019
    Posts:
    4
    Thanks for the suggestion!
    But sadly, it still doesn't work. Debug.log(DamageText) still returns null and I still get the same error message. Any other potential solutions? Thanks!
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    That should work. The code's a bit strange, as it tries to load the same asset from the same location twice for no reason, but the path looks good.

    Unity 5 is a long time ago (2016), but the API you're working with hasn't changed. Could you show what the PopupDamageParent prefab looks like?
     
  5. frankxp70

    frankxp70

    Joined:
    Jul 7, 2019
    Posts:
    4
    Haha yea the code is a bit weird. The if statement was some debugging I was doing and forgot to delete. Whoops!

    Attached is a pic of the prefab as well as it's child (in case it matters).

    Thanks! Untitled.png Untitled.png
     
  6. IcePhoenix_0101

    IcePhoenix_0101

    Joined:
    Sep 9, 2017
    Posts:
    32
    Sorry for the late reply. If you still haven't solved the problem, the DamageAnim component is on the child, not the PopupDamageParent that we are loading. The script should be :
    Code (CSharp):
    1.  DamageText = Resources.Load<GameObject>("Prefabs/PopupDamageParent").GetComponentInChildren<DamageAnim>();
     
  7. frankxp70

    frankxp70

    Joined:
    Jul 7, 2019
    Posts:
    4

    A late reply is better than no reply! And no, I didn't solve the problem, just ignored it and worked on something else. Thank you so much for catching that! Solved my problem! Really appreciate it!