- Home /
Arrays and the inspector
Dear community,
I've got a question related to initialize arrays through the script so that they already have the values in the inspector before the game launches. (Aka: You can edit them on the spot, in the inspector.)
Now, when I read the question I don't understand myself what I'm exactly asking, so allow me to elaborate my question, lets say we got this piece of code:
// Lots of other code
var tileSignal : int = 5;
// Lots of more code
Now, if I would assign this script to a new Gameobject, it would automatically get the value 5 in the inspector.
However, when the variable is an array, I can't seem to be able to initialize it through the script, for example:
// Some strange code above
var arrayPosZ : float[];
// Lots of code below
I can't find a way to initialize this the same way as the non-array variable (everything that I've tried gives me errors), so that they are prefilled in the inspector when I attach the script to a new gameobject.
To be more specific, I currently got the following code, and use the Start function to make sure it is applied to all the gameobjects. However, this approach causes the inspector to be completely overridden and by doing so I can't add any "individual values" to gameobjects. I currently use the following code:
// How to initialize this color array?
// >> Currently doing it in Start, so the black colors in the inspector... Will be overridden.
var tileColors : Color[] = new Color[4];
// What are our orders from our superiors? (Aka: What color will we become, or will we deselect?)
// >> If 4 is given as order, it means deselect.
var tileSignal : int = 5;
function Start () {
// Initializing color array.
tileColors[0] = new Color(0.2,0.6,1.0,1.0); // Light Blue, means moveable.
tileColors[1] = new Color(1.0,0.4,0.4,1.0); // Light Red, we got an enemy upahead.
tileColors[2] = new Color(0.4,1.0,0.4,1.0); // Light Green, our allies are nearby.
tileColors[3] = new Color(1.0,1.0,1.0,1.0); // Default Color, this is either white or black (depending on where the Tile is)
}
All in all, my question could be phrased as the following: How do I prefil arrays like I did with the non-array integer? And in more specific, with colors.
Lots of thanks in advance, Lots of love too,
Jip & my little nephew Yue who made this really hard to type.
Answer by whydoidoit · Mar 15, 2014 at 11:40 AM
You need to initialize the array directly like this:
var tileColors : Color[] = [new Color(0.2,0.6,1,1), new Color(1,0.4,0.4,1.0)];
So much frustration, such a simple answer. So much thanks!
In a similar way, lets say we have a Vector3, it would work the same way?
var tileColors : Vector3[] = [new Vector3(0.2,0.6,1,1), new Vector3(1,0.4,0.4,1.0)];
Either way, Lots of thanks already!
What is the C# equivalent of this? I tried
public Behave[] movementScheme = new Behave[Behave.moveDown];
and
public Behave[] movementScheme = [Behave.moveDown];
Your answer
Follow this Question
Related Questions
Unexpected values at the inspector when initializing member values from built-in arrays 0 Answers
How to make a custom type's contents modifiable in an array by inspector? 1 Answer
How do I make a public array of arrays appear in the inspector? 1 Answer
Multidimentional Array for Inventory style system in inspector. 1 Answer