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 /
  • Help Room /
avatar image
0
Question by unity_HOGvjKFOT88PFw · Jun 12, 2020 at 10:02 AM · arrayrandomspawnprefabs

Random array with prefabs?

Hi I have 5 prefabs and i want to spawn them one by one randomly every 3 seconds. It doesn't matter if 2 prefabs of the same kind are spawn one after the otger, on the opposite i would like them to be able to get repeated. I managed to make 1 prefab spawn every 3 seconds but the moment i try to get the program to choose a random prefab to spawn every iteration it says that " NullReferenceException: Object reference not set to an instance of an object RandomColumnPool.Update () (at Assets/Assets/RandomColumnPool.cs:52)" and it wont even spawn one? I´m trying this:

  public int columnPoolSize = 5;
  public GameObject[] prefab;
  void Start()
 {
     prefab = GameObject.FindGameObjectsWithTag("column");
   for (int i=0; i< columnPoolSize; i++)
     {
         index = Random.Range(0, prefab.Length);
         columns[i] = (GameObject)Instantiate(prefab[index], objectPoolPosition, Quaternion.identity); //for each column it instantiates its position
      
     }
 }


Sorry in advance if the question is dumb this is my first game.

Thanks!

Comment
Add comment · Show 1
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 davidmaamoaix · Jun 12, 2020 at 01:00 PM 0
Share

You need to post more of your code, as the NullReferenceException happened in Update(), which is outside the code snippet you posted.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Galactic_CakeYT · Jun 12, 2020 at 02:42 PM

First, I assume that you have already created the IEnumarator already to spawn every 3 seconds. Also next time search your question, because this question is already answered, you do courses, and they ask you to solve questions like this (C# survival guide). But anyways I found something that can help you. https://answers.unity.com/questions/566508/need-to-spawn-a-gameobject-from-an-array.html

probably can help you. If you are having problems such as this, revisit the problem, break it down, and think about what it means, this is a good way to learn in the beginning.

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
avatar image
0

Answer by Owen-Reynolds · Jun 12, 2020 at 11:03 PM

Try breaking it into more lines:

 Debug.Log("In spawn loop, i="+i);
 index = Random.Range(0, prefab.Length);
 GameObject spawnMe = prefab[index];
 GameObject spawnedItem = Instantiate(spawnMe, objectPoolPosition, Quaternion.identity);
 columns[i] = spawnMe;

My guess is you might see an error on the last line because you didn't create enough columns.

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

234 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

spawning problems, using array for random objects,Spawning rand prefab from array, destroy when not in the view of camera, roll new one. 0 Answers

Need to randomly place Waypionts from an Array in an "closed" 2D space 0 Answers

How to pick two random gameobjects in an array at the same time? 1 Answer

Spawning objects at runtime in vertical-scroller game 0 Answers

C# Adding random values to array 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