Error: Moving the position of a GameObject in an Array

Hi all,

First of all, I am new to Unity and I am trying my best to learn by creating a simple card game using javascript. I searched in here for an answer to understand why the error I am getting with no luck :frowning:

I am creating an array which needs to be redimensioned, so I am using the Array() instead of the native one, and I am filling it in with some objects that I instantiate. When I try to access a GameObject from the array and move it somewhere I get the error:

‘position’ is not a member of ‘Object’.

This is the code (well the extraction of the code to do this action):

var cardDeck = Array();

var duration : float = 1.0;
var startPoint : Vector3;
var startTime : float;
var endPoint : Vector3;

startPoint = transform.position;
startTime = Time.time;
endPoint=Vector3(0,0,0);

cardDeck[0].position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);

The code I use to fill in the array is this one:

var cardObject: GameObject;
var newCard : GameObject = GameObject.Instantiate(cardObject,Vector3(-105,50,x),Quaternion.identity) as GameObject;
    cardDeck.push(newCard);

If I change the above code to this one:

var cardObjectToMove : GameObject;
cardObjectToMove = cardDeck[0];
cardObjectToMove.transform.position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);

I get the error: Implicit downcast from ‘Object’ to ‘UnityEngine.GameObject’ on the line:

cardObjectToMove = cardDeck[0];

So I think it identifies the object inside the array as Object instead of GameObject. And I do not know why (if I am instantiating it as gameobject) and how to fix that.

I would appreciate your help understanding and fixing what’s going on, thanks!

Array is a collection that stores arbitrary elements which base class is Object.
Therefore, you should explicitly cast down the object your are accessing to GameObject:

var cardObjectToMove : GameObject;
cardObjectToMove = (cardDeck[0] as GameObject);
cardObjectToMove.transform.position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);