Having problems with custom objects in my array - BCE0019
Hello,
I have my own custom class called Block, which looks like this:
class Block {
var v : Vector3;
function Block(V : Vector3) {
v = V;
}
}
If I create an instance like this, and print out v, it looks like this...
var myExampleBlock = new Block(Vector3(1,1,1));
Debug.Log(myExampleBlock.v);
...and it works.
But if I put it into array which I set up here:
var map = new Array ();
And add a new Block and print out v...
map.Add (new Block(Vector3(34, 0, 676)));
Debug.Log(map[map.length-1].v);
...it doesn't work :(. Instead I get this:
BCE0019: 'v' is not a member of 'Object'.
The class and object creation code are in different scripts and scenes, but this doesn't seem to make a difference to the first example.
So therefore I believe that it is something to do with me adding Block to my array map.
Thanks for the help! :)
I'm not into Unity script, but my guess is the array is of type object where v is a member of Block. you need to cast it first
Debug.Log(((Block)map[map.length-1]).v);
I'm not exactly sure what you mean, @hexagonius. Did you mean to create the object first, and then put it in the array? Here is what I tried:
var example = new Block(Vector3(1,1,1));
Debug.Log(example.v);
(works) ... continued ...
map.Add (example);
Debug.Log(map[map.length-1].v);
(same error)
Is this what you mean? If not, could you clarify?
One more question: is map[map.length-1]
the right way to index the last element?
Thanks for the effort :).