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 YTGameDevDave · Apr 02, 2017 at 09:23 PM · arraygameobjectsaddinginstantiatinggenerating

After days of looking for solutions, what's wrong with my array script?

I've tried multiple ways of writing the syntax. I'm on the verge of leaving Arrayl̶i̶s̶t̶ for rigid solutions and using list for more flexibility. Yet I have to solve this one or else I'll feel like I've failed. Please help.

Only one object gets spawned and the array length stays zero.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class isolatingproblems : MonoBehaviour {
 
     public GameObject cube;
 
     int limit = 9;
 
     public GameObject[] arrcube;
 
 
     void spawn () 
     {
         for (int i = 0; i < limit; i++) 
         {
             GameObject cubearray = Instantiate (cube, transform.position, Quaternion.identity);
 
             arrcube[i] = cubearray;
         }
     }
 
 
     void Update () 
 
     {
 
         if (Input.GetButtonDown ("Jump")) 
     
         {        
             spawn ();
         }
 
     }
 }
 
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

2 Replies

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

Answer by navot · Apr 02, 2017 at 10:13 PM

you need to initialize the array with

 arrcube = new GameObject[limit];

before you can put anything into the array. Once you call that, you get an array with 9 null elements, so you need should put it in the start method.

Comment
Add comment · Show 2 · 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 AlwaysSunny · Apr 02, 2017 at 10:48 PM 1
Share

Two points for OP real quick:

The type you're working with here is called an Array, not an ArrayList.

Generally, you should initialize a collection immediately prior to using it. The Start method is a valid place to put certain initializations - including this one - but it's good practice to initialize objects based on their scope. In other words, if the Spawn method will be called before any other method needs this array, you should initialize it in the body of the Spawn method. Just pointing this out so you don't think you NEED to initialize all objects in Start.

avatar image YTGameDevDave AlwaysSunny · Apr 02, 2017 at 11:27 PM 0
Share

Thanks! Though I'm well aware of program structure. I just really don't understand the logic in this solution. I'm happy it works but I'd rather understand it too. $$anonymous$$aybe I should read up on intialization in general...

avatar image
2

Answer by dCalle · Apr 03, 2017 at 06:48 AM

you didn't initialize arrcube... in your case that would

arrcube = new GameObject[9];

Then you can iterate thorugh the for loop like this:

 for (int i = 0; i < arrcube.length; i++) 
 {
     code
 }

Also you can use a

List<GameObject> arrcube = new List<GameObject>();

if you want the array to be flexible in size. (to hold more or less than 9).

To add a Gameobject to arrcube you'd then need to use: arrcube.Add(cubearray);

Comment
Add comment · Show 2 · 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 YTGameDevDave · Apr 03, 2017 at 04:43 PM 0
Share

Ah okay cool, I think I get it now, basically you have to initialize a length. I guess you could say that initialization forms the foundation of your function input?

Thanks a lot!

avatar image dCalle · Apr 03, 2017 at 08:28 PM 1
Share

Yes, you have to initialize a length.

Imagine you were a wizard.

writing public Unicorn[] stable; is like saying You have a stable that only keeps Unicorns.

now A Princess asks you

how many Unicorns can it keep?

and you say:

Up to 9 Billion!!!

now the Princess is all aflutter and wants to get to your Stable and ride one of your 9 Billion unicorns.

That is the latest moment you better make sure you really have a stable in your backyard, because you don't want to be called a pathetic charlatan and thrown into prison don't you? ( The Princess can be quite rancorous when it comes to lying)

So you write this: stable = Unicorn[9000000000000];(actually I don't know if trying to summon such a huge stable would rip a $$anonymous$$r into your dimension, but you are a Grand Wizard of magic Wonderland you'll figure the boundaries of your Realm ;-) )

Phew, alright you luckily built a stable that can hold exactly 9 Billion Unicorns in time.

Now the Princess asks you:

But where are they?

Jeez! you forgot to fill the stable with those gracious animals! but thanks to your magical wand you can conjure them at will. And make one pop up right in the first Space, of all colors of the rainbow of course, because you want to impress the Princess ;-)

stable[0] = new Unicorn(Color.Rainbow);

Congratulations

The Princess is cock-a-hoop and you saved your ass from being thrown to prison. You really are a grandmaster of wizardry!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a 2d array of gameObjects 2 Answers

How do I know where a randomly selected object is in an array? 1 Answer

Game Object Chains 1 Answer

Audio Visualisation 0 Answers

Multiple Fire Points 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