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 ArceusGP · May 18, 2018 at 03:23 PM · gameobjectinstantiatelistrandom

Instantiate random from a list gives error: "Reference not set to an instance"

Hello there. I'm having a problem. I'm new writing here, so I will try to be clear with this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObjectsSpawnController: MonoBehaviour {
 
     private GameEngine mEngine;
 
     List<GameObject> prefabList = new List<GameObject>();
     public GameObject bullet1;
     public GameObject bullet2;
     public GameObject bullet3;
     public GameObject bullet4;
     
     private float InstantiationTimer = 2f;
     public float InstantiatonFinal;
     Vector3[] positions;
     Vector3 pos1;
     Vector3 pos2;
     Vector3 pos3;
     Vector3 pos4;
 
     int prefabNum; 
    
   
     // Use this for initialization
     public ObjectsSpawnController(GameEngine engine)
     {
         mEngine = engine;
 
     }
 
     void Start()
     {
         prefabList.Add(bullet1);
         prefabList.Add(bullet2);
         prefabList.Add(bullet3);
         prefabList.Add(bullet4);
 
         prefabNum = UnityEngine.Random.Range(0, 3);
         positions = new Vector3[] {new Vector3(0f, 1.2f, 12.39f), new Vector3 (0f, 1.2f, -12.39f),
             new Vector3(19f, 1.2f, 0f), new Vector3(-19f, 1.2f, 0f)};
         pos1 = positions[Random.Range(0, positions.Length)];
         pos2 = positions[Random.Range(0, positions.Length)];
         pos3 = positions[Random.Range(0, positions.Length)];
         pos4 = positions[Random.Range(0, positions.Length)];
        
        
     }
     
     // Update is called once per frame
     public void Update () {
         
         CreatePrefab();
         
     }
 
     void CreatePrefab()
     {
         
         InstantiationTimer -= Time.deltaTime;
         if (InstantiationTimer <= 0)
         {
             var bullets = Instantiate(prefabList[prefabNum] , pos1, Quaternion.identity);
                        
             bullets.transform.parent = gameObject.transform;
             InstantiationTimer = InstantiatonFinal;
         }
     }
 }
 


What I'm trying to do is to randomly instantiate some objects to four places so in any of the 4 places could appear any of the bullets I'm trying to instantiate. The problem is in the line : var bullets = Instantiate(prefabList[prefabNum] , pos1, Quaternion.identity); in prefabList[prefabNum] where in console gives me the alert of: NullReferenceException: Object reference not set to an instance of an object ObjectsSpawnController.CreatePrefab () (at Assets/Scripts/ObjectsSpawnController.cs:69) ObjectsSpawnController.Update () (at Assets/Scripts/ObjectsSpawnController.cs:59)

How should I proceed? Thanks a lot

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Oliver1135 · May 18, 2018 at 04:16 PM

I dont really understand your code, its not doing what you say its doing. it's always using pos1 and randoming through your list of game objects for different kinds of bullet to spawn

I would recommend breaking the statement in order to find out what is null, the bullets might be null, the list might be null. even brute force can help pin down what might be null for you. Just explode out what you are trying to do and try to cover all cases

 void CreatePrefab()
      {
          
          InstantiationTimer -= Time.deltaTime;
          if (InstantiationTimer <= 0)
          {
              Debug.Log("b1"+bullet1);
              Debug.Log("b2"+bullet2);
              Debug.Log("b3"+bullet3);
              Debug.Log("b4"+bullet4);                 
              Debug.Log("pf1"+prefabList[0]);
              Debug.Log("pf2"+prefabList[1]);
              Debug.Log("pf3"+prefabList[2]);
              Debug.Log("pf4"+prefabList[3]);
              var bullets = Instantiate(prefabList[prefabNum] , pos1, Quaternion.identity);
                         
              bullets.transform.parent = gameObject.transform;
              InstantiationTimer = InstantiatonFinal;
          }
      }
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

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

Best way to instantiate lots of objects 0 Answers

Random select from array and spawn 1 Answer

How to randomly iterate through a list of objects without repeating? 3 Answers

How to stop instantiating a certain object / objects from a list . 3 Answers

Add force to each object individually from a list immediately after it has been instantiated. ,Applying force to a listed object after it has been instantiated 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