Search Unity

Resources.Load() Issue

Discussion in 'Scripting' started by Deleted User, Jan 28, 2015.

  1. Deleted User

    Deleted User

    Guest

    I have a Script inside a Scene that loads Prefabs from a Resources folder that I created.
    Inside this Resources folder I have 2 Prefabs (Lets call them Prefab 1 and Prefab 2 for simplicity's sake).
    I can successfully load Prefab 1 and set it to an empty GameObject in my Script.
    After I load Prefab 1 when I try to load Prefab 2 its value is null.

    Note:

    I have a Debug.Log() that logs Prefab 2 (To see its value) and before I load Prefab 1 it returns the correct GameObject value, but after I load Prefab 1 it returns null.

    Sample Code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. // I created this sample code that acts exactly like the original one.
    5. // (Taking out other parts of code that do not influence on the loading of Prefabs).
    6. public class Loader : MonoBehaviour
    7. {
    8.     // The object that holds this Script is destroyed and a new GameObject is
    9.     // instantiated and given this same Script but has its loadNext set to true.
    10.     public bool loadNext;
    11.  
    12.     public GameObject loadedObject;
    13.  
    14.     private void Start ()
    15.     {
    16.         Debug.Log(Resources.Load<GameObject>("Prefab 2"));
    17.  
    18.         if (!loadNext)
    19.         {
    20.             loadedObject = Resources.Load<GameObject>("Prefab 1");
    21.         }
    22.         else
    23.         {
    24.             loadedObject = Resources.Load<GameObject>("Prefab 2");
    25.         }
    26.     }
    27. }
    Any help much is appreciated. Thanks for reading.
     
    Last edited by a moderator: Jan 28, 2015
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    I think you might get better help if you post some snippets of code showing what you are attempting.

    Be sure to enclose it with the correct tags for formatting (the word code in square brackets; see the help files).
     
  3. Deleted User

    Deleted User

    Guest

    Post was updated.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    Are you perhaps forgetting to instantiate the loaded resources? I don't see that code anywhere... This will just create references in code to the on- disk assets, not add anything to the scene.