Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

found a bug instantiating gameobject from resources

Discussion in 'Editor & General Support' started by steveh2112, Feb 19, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    235
    i was struggling to get this code working
    Code (CSharp):
    1.  
    2.         _ActiveArrow = Instantiate(Resources.Load<GameObject>("Models/Ammo/Arrows/Arrow/Arrow"));
    3.        
    4.         Rigidbody arrowRB = _ActiveArrow.GetComponent<Rigidbody>();
    5.         if (!arrowRB)
    6.                  Debug.LogError("PlayerController FireArrow NO RB");
    The prefab Resources/Models/Ammo/Arrows/Arrow/Arrow.prefab has a rigidbody but it wasn't been loaded by
    GetComponent, i got null

    however, the .fbx file was in the same folder as the prefab. so i took a guess and moved the fbx to a different folder, then everything ok, RB is loaded

    it seems unity first choice on Resources.Load is the fbx, not the prefab, so maybe its not a bug, but cirtainly is confusing
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    8,759
    Are the FBX and prefab both named "Arrow"? If so this doesn't sound like a bug. Just bad file naming.
     
  3. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    235
    yes, they are the same name and i don't consider that bad naming at all, i have hundreds of prefabs all named the same as the fbx, its my standard way of keeping track of things and keeping well organized. but putting the model in a sub folder called Models is not a bad idea

    anyhow, i think the fact that its not a documented 'feature' https://docs.unity3d.com/ScriptReference/Resources.Load.html is a fault with unity
     
unityunity