- Home /
'GetComponent' is not a member of 'Object'.
I know I need to force cast this line but don't know how
for ( var checkDoor : GameObject in cells[i].GetComponent(AIPathCellScript).doors)
// have tried this
for ( var checkDoor : GameObject in (cells[i].GetComponent(AIPathCellScript)as AIPathCellScript).doors)
// and also this
for ( var checkDoor : GameObject in cells[i].GetComponent.<AIPathCellScript>().doors)
and still coming out with the same error, Any Help would be greatly apperciated.
Answer by aldonaletto · Jul 08, 2013 at 04:38 PM
What's cells ? From the error message, I suspect that it's an Array (Array class, not builtin array): Array elements are defined as Object, the variant type that can hold values of any type. If this is the case, you should first get the cells element in a GameObject variable (supposing that each cell element is a GameObject), get the doors array reference and then enter the loop:
var cell: GameObject = cells[i];
var doors: GameObject[] = cell.GetComponent(AIPathCellScript).doors;
for (var checkDoor: GameObject in doors){
//
}
yes Cells is a Array class and Doors is another Array class in another script, and I'm trying to access that array with the for loop when I enter the code Aldonaletto gave meits giving me this error $$anonymous$$ identifier: 'cells'.
cells should be defined in your script - check spelling. Anyway, if doors is another Array, the assignment to a GameObject array will fail - declare doors as an Array:
var cell: GameObject = cells[i];
var doors: Array = cell.GetComponent(AIPathCellScript).doors;
for (var checkDoor: GameObject in doors){
//
}
You'll get compiler warnings about Implicit downcast from 'Object' to 'UnityEngine.GameObject', but the code should work.
Thank you its working now, Thanks for the fast reply as well.
Answer by asduffo · Jul 08, 2013 at 04:37 PM
Answer from a noob: is the content of the cells array a list of prefabs? If so, of course it happens because GetComponent is avaiabile only for GameObject and Component instances. Try to initiazlize the objects as GameObject using the method
list[i] = Initialize(Object object, Vector3 position, Quaternion.identity);
and then use GetComponent. It should work