Search Unity

Instantiating prefab game objects not workin

Discussion in 'Scripting' started by matias-e, Apr 2, 2015.

  1. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Hey. So, I have script that instantiates objects from an array randomly, but the Resources.LoadAll function is not finding the game objects for some reason. Debug.Log prints null. The folder name is correct (checked a bunch of times) and it is located at the root of resources. Here's the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PersonSpawner : MonoBehaviour {
    5.  
    6.     public GameObject[] personArray;
    7.     public Boundary boundary;
    8.  
    9.     void Awake () {
    10.  
    11.         personArray = Resources.LoadAll("Persons") as GameObject[];
    12.  
    13.     }
    14.  
    15.     void Start () {
    16.    
    17.         InvokeRepeating ("SpawnPerson", 0.0f, 1.5f);
    18.         Debug.Log(personArray);
    19.     }
    20.  
    21.     void SpawnPerson () {
    22.  
    23.         Vector3 randomPosition = new Vector3
    24.             (
    25.                 Random.Range (boundary.xMin, boundary.xMax),
    26.                 Random.Range (boundary.yMin, boundary.yMax),
    27.                 -1.25f
    28.             );
    29.  
    30.         if(CubeMover.keepClosed == false)
    31.         {
    32.             GameObject person = Instantiate
    33.                 (
    34.                     personArray[Random.Range(0,personArray.Length)],
    35.                     randomPosition,
    36.                     Quaternion.identity
    37.                 )
    38.                     as GameObject;  
    39.         }
    40.    
     
  2. Deleted User

    Deleted User

    Guest

    Can you try this instead and see if that changes anything? I vaguely remember having a similar issue but cannot for the life of me remember the fix, this may or may not be it as this will only load assets that are GameObjects, where as yours is returning ALL assets in the folder and if they are not a GameObject the "as" may be the cause of the null.

    Code (csharp):
    1. personArray = Resources.LoadAll<GameObject>("Persons");
    2.  
     
  3. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Hmm, the game objects are still not added to the array, but it changed the Debug.Log's output to "UnityEngine.GameObject[]". What could that indicate?
     
  4. Deleted User

    Deleted User

    Guest

    It would hopefully mean it has loaded something, so in that case we can check how many it has loaded.
    Code (csharp):
    1. Debug.Log(personArray.Length);
    If it's 0, then there isn't any GameObjects it can see in the folder given, which would be strange if you are definitely sure that there are.
     
  5. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Yup, it's printing 0. There are three game objects in the Persons folder, here's a screencap: http://i.imgur.com/xIHZzwr.jpg

    The PersonSpawner script is linked to the GameController game object that only has that one script on it.
     
  6. Deleted User

    Deleted User

    Guest

    Ooooooh I see the problem now, your folder is not in the correct place, that's showing as "Assets/Persons", to work with Resources it needs to be "Assets/Resources/Persons".
     
  7. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Ah! Thanks man, I'll try that out. :)
     
  8. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    Might I suggest this code as well:

    Code (CSharp):
    1. GameObject ObjVariable = (GameObject)Instantiate(Resources.Load(FolderInResources/Resource))