- Home /
A simple CS0309 problem...
It is a CS0309 problem that arises, and I am clearly not skilled enough to solve this problem myself.
GameObject me = (GameObject)Instantiate(Resources.Load("Card"));
me.GetComponent<Card>().DataName = "Info";
I need to assign a string to the variable DataName in the script Card,
on the object I just instantiated (which comes with Card script attached).
I really could use help to solve this.
Try this:
GameObject go = Instantiate(Resources.Load("Card")) as GameObject;
Card goCard = go.GetComponent<Card>();
if(goCard != null) goCard.DataName = "Info";
I tried your suggestion DajBuzi, but I still get error in what is equal to line 2 of those 3 lines you suggested, and line 2 in my initial code posted.
So the problem is with instantiating. Try this:
public GameObject go;
Now add object via unity editor.
GameObject goClone = Instantiate(go) as GameObject;
Card goCard = goClone.GetComponent<Card>();
goCard.DataName = "Info";
I immediatedly see that you forget I have to create the object in code as well, not in the Unity editor, as I need.
Yeah I know... here you try to help and I just act all stuborn :-)
Are you sure there is no namespace clash of some sort and that it's not finding some Card class that does not inherit from Component/$$anonymous$$onoBehaviour/etc?
Answer by AlkisFortuneFish · Feb 11, 2014 at 11:33 AM
Yep, as I thought, it is a namespace clash. Your DrawPile class from DrawPile.cs has a nested class called Card that is the same as the CardData class inside Card from Card.cs. When the compiler is told to look for Card, it GetComponent<Card>(), it does not find your main Card class, it finds DrawPile.Card, which is not a Component.
I would create a single class called CardData, move it into its own file (it does not need to be a MonoBehaviour or anything, just a plain data class) and use instances of that in both DrawPile and Card to store and pass around card data.
Let us know how you get on and if you are not sure about anything.
I was thinking he just needed a new string variable he could call "string cardName;"
then when he sets his variable to the string name he would just...
if(goCard != null) goCard.DataName = cardName;
@$$anonymous$$uPAFoo, depends on whether a card should be able to be stored and handled without being instantiated as a GameObject or not. If not, then yes I would dump the contents of the CardData into Card.
ehm... just like to add: it's DrawPile.cs, not DataPile. :-)
Not quite... look in the comments section before the answer I was about to 'accept'.
Yeah, I meant fixed the class name. Let's look at the problem above now.
Answer by DragonMind · Feb 11, 2014 at 11:16 AM
I made this answer to my own post, just because it seems that there is a cap to how many comments it shows of those added to a question/answer.
AlkisFortuneFish -> I read that you commented through a mail I got, but sadly I deleted the mail and there seem to be a cap on how many comments is shown to a question/answer.
Could you add the comment here.
I had to un-'accept' your answer AlkisFortuneFish, as the initial problem returned, I got this code: for (int c = 1; c < CardsInHand+1; c++) { GameObject me = (GameObject)Instantiate(Resources.Load("Card")); me.name = "Card IH " + c; CardData metoo = me.GetComponent(); metoo.DataName = "Now"+c; me.transform.position = new Vector3(myHand.transform.position.x - (CardsInHand*1.2f/2f) + (c-0.5f)*1.2f, myHand.transform.position.y, myHand.transform.position.z); }
The error is according to Unity is in the line with GetComponent. What a messy code setup it uses now.
Right, I see. Basically, what I meant is that you should have these following classes:
DrawPile -> Your old DrawPile but without the old nested Card class inside. Card -> Your old Card without the old nested CardData class inside. CardData -> The CardData class that used to live in Card.
So, do the following, or any part of it that you have not already done:
o Add a CardData field to your Card class public CardData ThisCard; o Add the Card script to your prefab.
Change the above code to:
for (int c = 1; c < CardsInHand+1; c++)
{
GameObject me = (GameObject)Instantiate(Resources.Load("Card"));
me.name = "Card IH " + c;
Card metoo = me.GetComponent<Card>();
// $$anonymous$$ake a new CardData or have one stored somewhere. If you haven't got all this data at this stage,
// you can add a default constructor to CardData, ie. public CardData() {}
CardData cd = new CardData("Now"+c, "WhateverRarity", "WhateverType", 0, 0, 0, 0, 0, 0, 0, 0);
metoo.ThisCard = cd;
me.transform.position = new Vector3(myHand.transform.position.x - (CardsInHand*1.2f/2f) + (c-0.5f)*1.2f, myHand.transform.position.y, myHand.transform.position.z);
}
Also, if you feel that this is messy and do not see yourself needing to store any cards without having them instantiated into the scene, you can dump the CardData members into the card class (not including the constructor) and access them directly, like $$anonymous$$uPAfoo said.
The GetComponent still doesn't work, getting the error code that this 'Answer' has as title.