- Home /
IOS Port Error : "GetComponent is not a member of Object"
heres my code snippet:
#pragma strict
function SelectPiece(){
// select piece to play
var pieceScore : int[]; // piece move value/score
pieceScore = new int[4]; // new array
var pValue : int; // move value
var last : int; // last piece value
var piece : int; // piece to play (piece is declared as an int - does unity cast this to object by default? )
switch(game.turn){ // select playing player
case PlayerTurn.P1:
if(gameScript.players[1] == Player.cpu){ // if CPU player
for(var x = 0;x < 4;x++){ // loop pieces
pieceScore[x] = GetPieceScore(gameScript.p1pieces[x]); // get piece score
}
for(x = 0;x < 4;x++){ // loop pieces scores
pValue = pieceScore[x]; // get piece value
if(pValue > last){ // if this piece has greater value then last piece
last = pValue; // set new last best score
piece = x; // set playing piece
}
}
isSelect = true; // set piece selected
gameScript.p1pieces[piece].GetComponent("piece1").Click(PlayerTurn.P1); // i get the error here// CPU play/click on selected piece
}
break;
i dont see anyway of casting this to a gameObject without getting the error "unity cannot convert int to UnityEngine.GameObject"
Please help!!! I know there are other post relating to this issue, but none of them directly addresses this issue. Im open to doing hella more research so if there are any links id be happy to explore them. I actually want to learn why this happens also or is it just IOS not supporting dynamic typing?
Thanks Guys!
I formatted your code for you. Please learn how to do this for yourself.
What is the type of the p1pieces
array? The compiler doesn't know what type it is.
here is how i have declared it in my game.js script
var p1pieces : Array;
thanks for helping with the format. i will learn how to do it myself
piece1 is also explicitly declared as type GameObject
found a way around the error and it works. So i thought i'd share it.
var temp1 = gameScript.p1pieces[piece] as GameObject;
(temp1.GetComponent("piece1") as piece1).Click(PlayerTurn.P1);
Answer by Eric5h5 · Jun 16, 2013 at 01:17 AM
Never use the Array class; it's slow and untyped. Use built-in arrays or generic Lists instead. Also, never use quotes in GetComponent. That makes it return Object instead of the correct type, among other problems. Just do GetComponent(Piece1)
.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Problems compiling script for iPhone that don't appear when running in unity. 2 Answers
How to tilt the gameobject based of Input.Acceleration? 0 Answers
EventSystem IDragHandler not working on iOS 1 Answer
dynamic typing in Unity iOS: ...not a member of 'UnityEngine.Component' 2 Answers