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 Jun 03, 2015 at 05:27 AM by galaboy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by galaboy · May 25, 2015 at 10:39 AM · arraysscriptingbasicsscritping

instantiating prefabs from array

hi, am trying to instantiate prefabs from array. i don't get any errors in the script, but when i run the script the prefabs assigned in the inspector displays empty. need help on how to use arrays. thanks in advance.

 using UnityEngine;
 using System.Collections;
 
 public class spawner : MonoBehaviour {
 
     //public GameObject card;
     //public GameObject card2;
 
      public GameObject[] prefabPool;
 
     // Use this for initialization
     void Start () {
 
         prefabPool = new GameObject[prefabPool.Length];
         //int index = Random.Range(0,prefabPool.Length);
         prefabCreation ();
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     public void prefabCreation()
     {
         for(int i = 0; i >= 4; i++)
         {
             for(int j = 0; j >= 4; j++)
             {
                 //Instantiate(prefabPool[prefabIndex]);
                 Instantiate(prefabPool[prefabPool.Length]);
             }
         }
     }
 }
Comment
Add comment · Show 4
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 Mehul-Rughani · May 25, 2015 at 10:43 AM 2
Share

just remove the line no 14 prefabPool = new GameObject[prefabPool.Length];

Hope it will works

avatar image HarshadK · May 25, 2015 at 10:49 AM 1
Share

Also not sure what you are trying to do inside your PrefabCreation() method since:

 Instantiate(prefabPool[prefabPool.Length]);

will result in Index Out of Range Exception. Plus looks like you are trying to achieve a grid based instantiation looking at your two for loops. If so you might also want to change the position at which the prefabs are instantiated based off the values of 'i' and 'j'.

avatar image QUANARMY · May 25, 2015 at 11:03 AM 1
Share

prefabPool = new GameObject[prefabPool.Length]; => your array is empty!

avatar image galaboy · May 25, 2015 at 11:05 AM 0
Share

am trying to do a matchup the cards type game, where i have to match two similar cards having same image. initially iam trying to test the code with 2 prefabs. i need to instantiate these prefabs initially two pairs of each(4) so i could match them. need help on the script how to instantiate from an array.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by NikunjPopat · May 25, 2015 at 10:53 AM

 public int numberOfPrefabs=5;
 public GameObject[] prefabPool;
 
 void Start()
 {
      prefabPool=new GameObject[numberOfPrefabs];
      prefabCreation();
 }
 
 public void prefabCreation(){
      for(int i=0;i<prefabPool.Length;i++){
        Instantiate(prepfabPool[i]);
      }
 }
Comment
Add comment · Show 3 · 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 · May 27, 2015 at 08:41 AM 0
Share

thanks all for the help.

avatar image NikunjPopat · May 28, 2015 at 06:12 AM 0
Share

Welcome :) Hakuna $$anonymous$$atata :)

avatar image galaboy · May 28, 2015 at 06:36 AM 0
Share

also need another help. i want all the prefabs to be instantiated at the center of the screen. do u have any idea how to do this. for example if i spawn at 0th point in the screen the following prefabs will spawn adding next to the first resulting in going out of camera.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

transform.rotation = Quaternion.Slerp "freaks out doesn't acutely track game object. 1 Answer

Script error about the semicolon. 1 Answer

how to instantiate gameobjects in a 2d array. 2 Answers

need help on prefab spawning 1 Answer

Can someone reshape my script so that it is more clean and tidy? it works just looks ugly 3 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