- Home /
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 ();
}
}
}
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.
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.
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...
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);
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!
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
Follow this Question
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