- Home /
'name' is not a member of Object (js)
Hello all, first off I'm just learning Unity so I'm probably doing something dumb, but: this code was working before, now I have to comment out the lines below to still instantiate correctly (which is place 4 rows of cards, each 12 cards high ), otherwise it only puts one card down instead of 48. ** When the commented sections below wasn't throwing an error, all that was done was removed the 'clcne' in hierarchy and tacked on a name (no big deal), but the If statement was only allowing the first row of cards to be clicked on (disabled script in all other instantiated cards) which is core functionality.
[code] #pragma strict var cardCounter = 0;
function CreateLeftLane (lanename) {
var i : int = 0;
var pos = .2;
var cardNumber = 1; // assigning numbers, 1-12+ going up row
for (i = 0; i < laneLength; i++)
{
//pick a random item in array
var randNum = Random.Range (0, (prefabArray.Length));
var randCard = prefabArray[randNum];
Debug.Log ("Rolled a " + randNum);
Debug.Log ("Card is " + randCard);
var camerapos = Vector3(.34, 1.58, 0);
var cardstartpos = Vector3(pos,.8,.4);
if (lanename == "one")
{
cardstartpos = Vector3(pos,.8,.4); //place lane 1
var cardLane = "LaneA-";
}
else if (lanename == "two")
{
cardstartpos = Vector3(pos,.8,.15); //place lane 2
cardLane = "LaneB-";
}
else if (lanename == "three")
{
cardstartpos = Vector3(pos,.8,-.15); //place lane 3
cardLane = "LaneC-";
}
else if (lanename == "four")
{
cardstartpos = Vector3(pos,.8,-.4); //place lane 4
cardLane = "LaneD-";
}
//var newCard : Transform;
var newCard = Instantiate(randCard, cardstartpos, Quaternion.AngleAxis(270, Vector3.up));
// newCard.name = newCard.name.Replace("(Clone)","").Trim(); // take "Clone" off
// newCard.name = newCard.name + " " + cardLane + cardNumber; // add a number to it
// if (cardNumber != 1) { //only card on first row clickable
// newCard.GetComponent(FliponClick).enabled = false;
// }
cardNumber++;
pos += .2;
cardCounter++; // track # of cards
}
}
[/code]
as a note, if uncommented I get these 2 errors: 'name' is not a member of 'Object'. or 'GetComponent' is not a member of 'Object'.
Answer by fafase · Jun 16, 2014 at 09:57 PM
line 35:
var newCard :GameObject = Instantiate(randCard, cardstartpos, Quaternion.AngleAxis(270, Vector3.up));
Instantiate returns an Object, since UnityScript does the typing for you, newCard ends up being an Object instead of GameObject. Explicitely defining it for GO does the cast and makes it a GO.
C# would not have let you get away with it in the first place.
Actually C# does let you use the var type. I'm pretty sure you could get the same problem in c#.