- Home /
Getting information from an array
Hello everyone, I have a bit of a problem on my hands.
I have created a grid of points using a 2d array and that part works fine, now for each point I have a script with an int variable and I want that variable filled with information from another array for each point.
part of the code:
#pragma strict
var starPoint : GameObject;
var noOfStars : int;
var milkyWay : int[] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 8, 12, 13, 12, 8, 0, 0, 11, 14, 15, 24, 23, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 8, 14, 16, 17, 16, 14, 8, 0, 16, 25, 27, 25, 16, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 10, 14, 15, 14, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 5, 22, 27, 28, 24, 18, 16, 12, 0, 17, 25, 38, 37, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var width : int = 25;
var length : int = 25;
var gridPoint : GameObject [,];
function Awake ()
{
gridPoint = new GameObject[width, length];
for (var x : int = 0; x < width; x++)
{
for (var z : int = 0; z < length; z++)
{
var gridPointCentre : GameObject;
if (milkyWay [z * 20 + x] >= 0)
{
gridPointCentre = starPoint;
}
gridPoint[x, z] = Instantiate (gridPointCentre, Vector3(x * 100, 0, z * 100), Quaternion.identity);
}
}
}
Now the value I want to fill the int variable with in the other script for each object is from the milkyWay array so that every object has a value i.e object 405 : starNumber = 5, object 406 : starNumber = 22, object 407 : starNumber = 0 etc etc, I tried to iterate through the array using a for loop but weirdly every value came back as "2" or "9".
Now what I want to happen is for that value to then be divided by 4 to give a final value that can then be used for instantiation.
Problem is, how do I do this?
Many thanks
I'm not fully sure what you're trying to do... "for each point I have a script with an int variable and I want that variable filled with information from another array for each point". – Does that mean you have a script for every point in the array or you have two arrays or...? Try and clarify what you're trying to do.
Sorry, i'll try to be more clear.
Basically i have two scripts, one to generate a grid of empty gameobjects, the other script is attached to the gameobject prefab that deals with creating objects from a number.
Now the grid i have set up, however what i now what to do is take the information from the milkyWay Array and place that information in the empty gameobjects or gridpoints, there are over 16,000 values in the array and 16,000 empty game objects, each gameobject has a variable in a script that i want filled with a number from the array.
Problem is, i got stuck while doing it, I tried a for loop to add one number to one one gameobject but i got some strange results.
Answer by Essential · Nov 14, 2013 at 09:59 PM
var starPrefab : GameObject;
private var width = 128;
private var height = 128;
function Awake ()
{
for ( var w=0; w < width; w++ )
{
for ( var h=0; h < height; h++ )
{
var go = Instantiate ( starPrefab, Vector3(w*10, h*10, 0), Quaternion.identity );
go.GetComponent(StarScript).brightnessValue = milkyway[(w*height)+h];
}
}
}
Keep in mind this requires a milkyway array that contains 128*128 (16384) values in it or it will give an error.
I suggest that once you've Instantiated this set of gameobjects you save it to the scene, so it doesn't need to get Instantiated every time it's run. For that you would move the generation code from Awake() into an editor script. How to do this can be found on Google.
The code is really that short? Blimey!
I'll give it a try later tonight as i'm getting sorted out for work but i'm pretty sure getting that amount of values won't be a problwm, will probably just be a case of adding a few extras 0's.
What you want is pretty simple. Study the code and follow what it's doing…
The width loop goes along sideways once then the height loop begins. Each step of the height loop goes down one and then instantiates a star at that point, it also gets that new star's script (I assume you've given the star prefab a script called StarScript
or something similar), and then sets the brightnessValue
var within the StarScript. Then the height loop steps down one and creates another star in the same manner. When the height loop reaches the bottom, the width loop take another step sideways and the height loop starts from the top again.
But if I were you, I'd start with just a few stars (8 width x 8 height = 64 stars). Once that works you can consider going to 16,000.
Haha, sorry, i should have said that i'd read the code, i just didn't know how to retrieve information from an array and put it into another script.
But yeah, i was planning on starting small as i'm not sure performance wise how it's going to handle it.
Thank you though for the help.
Just tried the code with a 30x30 grid and the 900 starPrefab's (as you called it) are created fine, however the brightnessValue (as you called it) remains at zero for each one?
Could it be a problem with the milkyWay array I have it set as var milkyWay : int[] = [all the numbers];
Hmm... try adding 'private' in front of the milkyWay var.
In fact, it's good practice to have all variables as private unless you need them to be accessible from elsewhere.