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 /
  • Help Room /
avatar image
0
Question by lordkrups · Apr 05, 2019 at 11:07 AM · prefabarrayinstantiate prefab

Spawning prefab to an array, but array is empty and prefab is just in hierarchy.

Thanks in advance for your help!

What I want to do is spawn a random foodPrefab when I press a button. That food prefab should then go in to the foodArray. I have 4 foodPrefabs referenced from the inspector. They spawn but the array is never populated(stays zero) unless I manually increase the number in the inspector.

I tried to search for other similar questions but none of the answers I found worked for my solution. If you can link me a answer or tell me what I'm doing wrong I would appreciate it. Thanks again!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnFood : MonoBehaviour
 {
 
     [Header("Food Attributes")]
     public GameObject[] foodPrefabs;
 
     public GameObject[] foodArray;
 
     public int spawnedFood = 0;
 
     public Transform spawnPoint;   
 
     void GetSpawnPoint()
     {
         int randomSP = Random.Range(0, SpawnPoints.spawnpoints.Length);
         spawnPoint = SpawnPoints.spawnpoints[randomSP].transform;
     }
     //On Button Press
     public void SpawnRandomFood()
     {
          //Get somewhere to spawn the food
         GetSpawnPoint();
         //Select a random food
         int randomFood = Random.Range(0, foodPrefabs.Length);
 
         foodArray[spawnedFood] = Instantiate(foodPrefabs[randomFood], spawnPoint.position, spawnPoint.rotation) as GameObject;
 
         spawnedFood++;
 
 
     }

`

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 Chris333 · Apr 05, 2019 at 12:26 PM 0
Share

Where do you initialize the length of the foodArray?

Does it need to be visible in the inspector?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Chris333 · Apr 05, 2019 at 12:38 PM

If the foodArray need not to be visible in the inspector and it shouldn't have a fixed size you should use a list instead of an array. You have to declare a size for an array and they can't increase dynamically and you don't know how often the user will press the button. List's can increase dynamically and the user can press the button as often as she likes.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnFood : MonoBehaviour
 {
 
 [Header("Food Attributes")]
 public GameObject[] foodPrefabs;
 
 public List<GameObject> foodList = new List<GameObject>();
 
 public int spawnedFood = 0;
 
 public Transform spawnPoint;
 
 void GetSpawnPoint()
 {
     int randomSP = Random.Range(0, SpawnPoints.spawnpoints.Length);
     spawnPoint = SpawnPoints.spawnpoints[randomSP].transform;
 }
 //On Button Press
 public void SpawnRandomFood()
 {
     //Get somewhere to spawn the food
     GetSpawnPoint();
     //Select a random food
     int randomFood = Random.Range(0, foodPrefabs.Length);
 
     foodList.Add(Instantiate(foodPrefabs[randomFood], spawnPoint.position, spawnPoint.rotation) as GameObject);
 
     spawnedFood++;
 }


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

209 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

Related Questions

All instantiated objects having same location, 0 Answers

Attacking an enemy (2D platformer) 0 Answers

How to Spawn a Prefab at an already Spawned Prefab's that's from an array at their transform.position? 1 Answer

problem with prefab's script 1 Answer

How can I get a reference to a Prefab in the Asset folder without instantiating it? 2 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