Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DragonMind · Feb 10, 2014 at 08:46 PM · getcomponent

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.

Comment
Add comment · Show 19
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DajBuzi · Feb 10, 2014 at 08:56 PM 0
Share

Try this:

 GameObject go = Instantiate(Resources.Load("Card")) as GameObject;
 Card goCard = go.GetComponent<Card>();
 if(goCard != null) goCard.DataName = "Info";
  
avatar image DragonMind · Feb 10, 2014 at 10:09 PM 0
Share

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.

avatar image DajBuzi · Feb 10, 2014 at 10:16 PM 0
Share

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";
avatar image DragonMind · Feb 10, 2014 at 10:24 PM 0
Share

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 :-)

avatar image AlkisFortuneFish · Feb 10, 2014 at 10:58 PM 1
Share

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?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KuPAfoo · Feb 11, 2014 at 12:11 PM 0
Share

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;
avatar image AlkisFortuneFish · Feb 11, 2014 at 12:24 PM 0
Share

@$$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.

avatar image DragonMind · Feb 11, 2014 at 01:39 PM 0
Share

ehm... just like to add: it's DrawPile.cs, not DataPile. :-)

avatar image DragonMind · Feb 11, 2014 at 01:50 PM 0
Share

Not quite... look in the comments section before the answer I was about to 'accept'.

avatar image AlkisFortuneFish · Feb 11, 2014 at 01:55 PM 0
Share

Yeah, I meant fixed the class name. Let's look at the problem above now.

Show more comments
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DragonMind · Feb 11, 2014 at 10:05 AM 0
Share

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.

avatar image DragonMind · Feb 11, 2014 at 01:49 PM 0
Share

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.

avatar image AlkisFortuneFish · Feb 11, 2014 at 02:12 PM 0
Share

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);
 }
avatar image AlkisFortuneFish · Feb 11, 2014 at 02:29 PM 0
Share

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.

avatar image DragonMind · Feb 11, 2014 at 02:33 PM 0
Share

The GetComponent still doesn't work, getting the error code that this 'Answer' has as title.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing other objects efficiently 1 Answer

Destroy(GetComponent(...)) not working 1 Answer

GetComponent(); in Awake cant be accessed in Update 1 Answer

BCW0012: WARNING: 'UnityEngine.MeshCollider.mesh' is obsolete. mesh has been replaced with sharedMesh and will be deprecated 1 Answer

Problem getting information out of colliding objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges