Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Kasei-hime · Jan 05, 2019 at 04:45 PM · prefabarray of gameobjects

Instantiating a prefabs from an array of prefabs gives a NullReferenceException error

I'm trying to create an array of prefabs from my prefabs folder so that whenever a new enemy is added (and there will be a lot, so I don't want to have to add each one in the inspector manually), I can instantiate it from the array. In my script, I'm attempting to instantiate random enemies from the array, but instead, it gives me a NullReferenceException error, telling me that the object reference is not set to an instance of an object. How do I get it to use to random prefabs selected in the array for the object reference ? Here's the code :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemySpawner : MonoBehaviour
 {
     public GameObject ActiveEnemy, LeftSupportEnemy, RightSupportEnemy, InactiveEnemy; //the four positions of the enemies I'm trying to spawn
     public GameObject[] allLeps; //the gameobject array
     public int totalLeps = 0; //an int to keep track of the array size
     public GameObject EAttackPos, ELeftSupPos, ERightSupPos, EInactivePos; //use for their transform.positions later, these are set in the inspector
 
     private void Start()
     {
         allLeps = Resources.LoadAll<GameObject>("Prefabs") as GameObject[]; //this seems to be working properly since I can see the array getting populated by prefabs in the inspector
         foreach (GameObject leps in allLeps)
         {
             totalLeps++; //this number goes up to the expected amount
         }
     }
 
     public void SetRandomOpponents() //this gets called in a seperate BattleManager script
     {
         Debug.Log("Starting to set opponents"); //for testing purposes. It doesn't even show up, the nullreferenceexception happens first.
         int E1 = Random.Range(0, totalLeps);
         int E2 = Random.Range(0, totalLeps);
         int E3 = Random.Range(0, totalLeps);
         int E4 = Random.Range(0, totalLeps); //getting a random position in the array
 
         GameObject ActiveEnemy = Instantiate(allLeps[E1]) as GameObject;
         GameObject LeftSupportEnemy = Instantiate(allLeps[E2]) as GameObject;
         GameObject RightSupportEnemy = Instantiate(allLeps[E3]) as GameObject;
         GameObject InactiveEnemy = Instantiate(allLeps[E4]) as GameObject; //trying to make it so that it instantiates a random gameobject from the array, but they aren't being set
 
 
         
         ActiveEnemy.transform.position = new Vector2(EAttackPos.transform.position.x, EAttackPos.transform.position.y);
         LeftSupportEnemy.transform.position = new Vector2(ELeftSupPos.transform.position.x, ELeftSupPos.transform.position.y);
         RightSupportEnemy.transform.position = new Vector2(ERightSupPos.transform.position.x, ERightSupPos.transform.position.y);
         InactiveEnemy.transform.position = new Vector2(EInactivePos.transform.position.x, EInactivePos.transform.position.y); //this is just to give their their proper positions
 
     }
 
 
 }
 
Comment
Add comment · Show 3
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 sean244 · Jan 05, 2019 at 07:14 PM 0
Share

Can you post your Battle$$anonymous$$anager script?

avatar image JxWolfe · Jan 05, 2019 at 07:19 PM 0
Share

why use totalLeps? just use allLeps.Count()

avatar image JxWolfe · Jan 05, 2019 at 07:21 PM 0
Share

please post Battle$$anonymous$$anager script, if the nullreferenceexeption happens first, then that code may be fine...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ady_M · Jan 06, 2019 at 03:50 AM

Try this:

 Debug.Log (string.Format ("E1-E4: ({0} , {1} , {2} , {3})  --  Max Index: ({4})", E1, E2, E3, E4, totalLeps - 1));

 // Output example. Look for numbers greater than Max Index. Those are bad.
 // It's over 9000!!!
 // E1-E4: (0 , 9001 , 3 , 1)  --  Max Index: (3)



 

And this:

 for (int i = 0;  i < allLeps.Length;  i++)
 {
     Debug.Log (string.Format ("Lep Reference #{0} is {1}",  i,  allLeps[i] != null ? "OK" : "Null"));
 }

 // Output example
 // Lep Reference #0 is OK
 // Lep Reference #1 is Null
 // Lep Reference #2 is OK
 // Lep Reference #3 is OK

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 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 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 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 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 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

Array values changing after gameObject is instantiated from prefab 1 Answer

Obtaining the Variable of a Prefab in an array. 1 Answer

Adding a prefab to GameObject[] via script 1 Answer

Why does each instance of my Prefab increase drawcalls? 1 Answer

Activating a prefab in the scene by find it over a tag? 1 Answer


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