Search Unity

instantiating a prefab from resources is missing child

Discussion in 'Scripting' started by steveh2112, Jan 27, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i used to make my prefabs definitions in the script public and set up in the inspector, but i was getting publics bloat so i switched to this
    Code (CSharp):
    1.      _ArrowTOPrefab = Instantiate(Resources.Load<GameObject>("Models/Ammo/Arrows/Arrow/ArrowTO"));
    and it instantiates the prefab ok, except the prefab has a child and that child doesn't get instantiated, the child count is 0

    any idea why and how to fix it? (beside going back to the public)

    thx
     
  2. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    This sounds like a bug in Unity. But it may not be.
    1. What version are you using?
    2. Does it happen on other Unity versions?
    3. (Most important) If you replace
    Resources.Load<GameObject>("Models/Ammo/Arrows/Arrow/ArrowTO")
    with the public prefab property pointing at the same object, you get the correct result? Try it now - maybe you've changed something else that caused it.
    4. Last resort is closing Unity, deleting the Library folder and re-opening it
     
  3. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    V 2019.2.19f1 personal
    don't know about other unity versions, just tried it for first time
    i've already gone back to the 'public GameObject thisThatAndTheOther;' and it works fine
    i'll try deleting the Library folder i guess, is that ok? is it autogenerated?

    thx
     
  4. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    Yes, that's auto-generated. Just make sure Unity is closed. And also try it on other unity versions - maybe it's something buggy with just this one. Worth a shot.
     
  5. Nano_95

    Nano_95

    Joined:
    Dec 25, 2017
    Posts:
    7
    hey steveh. I know this is late, but im running into this problem too! I tried deleting the Library folder as suggested. did you ever figure out the issue with this?
     
  6. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    yes, i thought i posted something already. the problem is if you have an fbx file in the same folder as the prefab with, with the same name, unity will pick the fbx first. just rename or move the fbx file to a folder called model or something like that, and the problem will go away
     
  7. Nano_95

    Nano_95

    Joined:
    Dec 25, 2017
    Posts:
    7
    Hmm i'm not sure I know what an FBX file is. Nonetheless, i do not see any .FBX files. However, I moved the prefab to a different folder instead of what you suggested (moving the fbs to a different folder) and tried again, but unfortunately, i'm still having my prefab not get instantiated with the child... this is bizarre
     
  8. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    sorry, don't know then, the fbx file is the 3d model that the prefab is based on. check you don't have anything in the folder with the prefab that is the same name. if that's not it, then no idea, sorry
     
  9. Nano_95

    Nano_95

    Joined:
    Dec 25, 2017
    Posts:
    7
    I'm shaking my head quite hard... turns out all of the advice of using GameObject.Find were true. My issue was that the game was adding (clone) to the string when it was creating it, and therefore my find did not work. so that's great. I was doing this in a try catch and forgot to uncomment my Debug.Log, so i was unable to see any error messages.

    I do appreciate your time nonetheless, and hope this does help someone some day... be it your fix or mine, lol (avoid .Find())