Search Unity

How to get reference to a script component despite its name C#?

Discussion in 'Scripting' started by Hosky, Dec 19, 2016.

  1. Hosky

    Hosky

    Joined:
    Nov 30, 2016
    Posts:
    4
    Hey all,


    I'm making a card game. Each card has an individual script which holds that card's value and details. It's not efficient but its what I'm working with at the moment.

    I would like to get a reference to the script component on each card so I can drill down to the cardValue variable in each.

    I've tried cardScript = GameObject.FindObjectOfType(Script);

    But Script doesn't exist in the current context.
    Thanks
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    how else would you store instance specific data per card? :confused: or do you mean you have a script called "Ace Diamonds" then another called "Two Diamonds", then another called "Three Diamonds" (if so, this is where your problem is, don't "work around it" fix this first)? :eek:

    then you'll be wanting "FindObjectsOfType" to pick up all of them.

    "script" in you example is the name of the script file you've created (hopefully something like "Card")

    there are several variants for these kinds of functions, I prefer the generics so

    Code (csharp):
    1.  
    2. Card[] cards = FindObjectsOfType<Card>();
    3.  
    but there is also
    Code (csharp):
    1.  
    2. Card[] cards = FindObjectsOfType(typeof(Card));
    3.  
    and probably one which works off the string, but ewww to that one.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Polymorphism is your friend here. You want each individual card to either inherit from a common base, or implement a common interface.
     
    Xepherys likes this.
  4. Hosky

    Hosky

    Joined:
    Nov 30, 2016
    Posts:
    4
    I don't think that'd what I'm after. It's a better way but for now I'd like a way to get a reference to the script on the card object. As you said they all have different names such as twodiamonds and threediamonds. They only have one script per card object. Is there a way.
     
  5. Hosky

    Hosky

    Joined:
    Nov 30, 2016
    Posts:
    4
    Yeah definately. Version 2 will have more polymorphism.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Polymorphism is your friend here. You want each individual card to either inherit from a common base, or implement a common interface.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you don't want to do that, you are looking at a long hard slog involving reflection and type checking and all sorts of crazy nonsense. You really don't want to go down that path.

    Stop. Refactor your code to provide a single interface or base class. Then continue.
     
  8. Hosky

    Hosky

    Joined:
    Nov 30, 2016
    Posts:
    4
    Okay, it will be better in the long run. Thanks for your help.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It will also be better in the short run. Trust me. Its faster to do it this way then any alternative you can devise.
     
  10. Xepherys

    Xepherys

    Joined:
    Sep 9, 2012
    Posts:
    204
    You would need to call something like:

    cardScript = GameObject.FindObjectOfType(<ClassNameFromScript>);

    So, if the script on one card was called SuperCoolCard, even if it inherits from a MonoBehaviour, like SuperCoolCard : MonoBehaviour, you would call:

    cardScript = GameObject.FindObjectOfType(SuperCoolCard);

    But then you wouldn't be able to use the same call for each card. This is why people keep saying that you need to use polymorphism.