Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 18, 2015 at 07:17 AM by galaboy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by galaboy · Sep 15, 2014 at 11:06 AM · aiscriptingbasics

loading prefabs in an array

hi need to load all the prefabs in the resources folder into an array. the script is below. the problem is when i run the script it shows an error message as below. i dont know where am making mistake. need help on how to load the prefabs.

NullReferenceException: Object reference not set to an instance of an object runtime.Start () (at Assets/scripts/runtime.cs:22)

 using UnityEngine;
 using System.Collections;
 
 public class runtime : MonoBehaviour {
 
     public int rows;
     public int columns;
     public int n = 3;
 
     int initialPositionX = -5;
     int initialPositionY = 5;
     float bufferSpace = 1.5f;
 
     public Transform prefab;
     public GameObject[] items;
     
     // Use this for initialization
     void Start () {
         items = new GameObject[];
         items = Resources.LoadAll("prefabs")as GameObject[];
         //Debug.Log(items.Length);
         int index = Random.Range(0, items.Length);
         cardCreation(index);
     }
     
     // Update is called once per frame
     void Update () {
             
     }
 
     public void cardCreation(int cardIndex)
     {
           float spaceBetweenCards = prefab.transform.localScale.x;
 
         for(int i = 0; i < n; i++)
         {
             for(int j = 0; j < n; j++)
             {
 
                 Instantiate(items[cardIndex], new Vector3((j*bufferSpace+spaceBetweenCards),(i*bufferSpace+spaceBetweenCards),0),
                             Quaternion.Euler(270,0,0));
             }
         }
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by HarshadK · Sep 15, 2014 at 11:27 AM

In your this line

 items = new GameObject[];

your array is not initialized. You should actually get an error stating:

CS1586: Array creation must have array size or array initializer

So you can set the length of your array dynamically as:

 items = new GameObject[Resources.LoadAll<GameObject>("prefabs").Length];

Or you can also specify the value for length there directly.

And you populate it using:

  items = Resources.LoadAll<GameObject>("prefabs")as GameObject[];

Also remember there should be a folder names 'prefabs' inside your Resources folder where all your prefabs will be stored. This 'prefabs' is the argument you pass to your Resources.LoadAll method.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image galaboy · Sep 15, 2014 at 01:02 PM 0
Share

when i do like this items = new GameObject[Resources.LoadAll("prefabs")as GameObject[]];

i get an error message stating Assets/scripts/runtime.cs(19,80): error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to int'

avatar image HarshadK · Sep 15, 2014 at 01:16 PM 0
Share

It is:

 Resources.LoadAll<GameObject>("prefabs").Length

that you put inside those square brackets.

avatar image galaboy · Sep 15, 2014 at 01:41 PM 0
Share

done working. but another error message stating

ArgumentException: The prefab you want to instantiate is null.

i have a folder named as prefab already. the path is correct.

avatar image HarshadK · Sep 15, 2014 at 01:57 PM 0
Share

i have a folder named as prefab already

but the argument has prefabs as the name.

Check for the spelling of the folder name.

avatar image RealSoftGames · Feb 02, 2017 at 12:00 PM 0
Share

i just had to say thank you for your answer, this was doing my head in for over an hour before i stumbled upon this. i initially tried doing this manually and loading each asset in independandtlyusing

 string[] directories = Directory.GetDirectories("Assets/Resources/Database");

gaining the directories of all my prefabs worked perfectly fine. its this next step.

which i put this in a loop

 List<GameObject> prefabsList = new List<gameObject>();
 
 
 foreach (string dir in directories)
 {
 GameObject[] prefabs = Resources.LoadAll(dir) as GameObject[];
 
 for(int i = 0; i < prefabs.Length; i ++)
 {
   prefabsList.Add(prefabs[i]);
 } 
 
 }

however i was more than sure this method would have worked but it seemed to throw an error with the loading of resources.

anyways enough yapping, i just wanted to say thank you for your answer as this really helped.

avatar image
0

Answer by Cherno · Sep 15, 2014 at 11:12 AM

It's possible, depending on the number of Prefabs that have to be loaded, that not all could be loaded into the array before cardCreation starts to try and access prefabs from the array. Maybe try starting cardCreation as a Coroutine with a 1-second delay or something. In anyway, if you don't call cardCreation at all, is the array successfully filled with Prefabs?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image galaboy · Sep 15, 2014 at 11:19 AM 0
Share

actually the array is not getting loaded. i manually connected the prefabs in the inspector, but when running the script the array shows zero elements. trying to figure out wat is the problem.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NPCs follow player in a line 1 Answer

Enemy frozen in place by light? 2 Answers

Coordinating multiple AI enemies 3 Answers

How to make basic AI in a 2d game? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges