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,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  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, 10, 11, 19, 17,  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,  0,  0,  0,  0,  0,  0,  0,  0, 10, 13, 14, 25, 24, 12,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  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.

1 Answer

1

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.

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.

Just noticed you are using an older version of my code from yesterday before I updated it. Convert this: go.GetComponent(StarCreation).starNumber = milkyWay[x+z]; to this: go.GetComponent(StarCreation).starNumber = milkyWay[(x*length)+z]; And it'll work fine :)