Search Unity

c# GetComponent string?

Discussion in 'Scripting' started by markrodgers11, Jul 31, 2013.

  1. markrodgers11

    markrodgers11

    Joined:
    Jul 10, 2013
    Posts:
    23
    Code (csharp):
    1.  
    2. instance.GetComponent<TYPE>().enabled = true;
    3.  
    how would I go about finding a component attached to the instance gameobject via a string?

    example:
    Code (csharp):
    1.  
    2. instance.GetComponent<STRINGHERE>().enabled = true;
    3.  
     
    Last edited: Jul 31, 2013
  2. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
  3. markrodgers11

    markrodgers11

    Joined:
    Jul 10, 2013
    Posts:
    23
    Type 'UnityEngine.Component' does not contain a definition for `enabled` and no extension method `enabled' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?
     
    Last edited: Jul 31, 2013
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
  5. markrodgers11

    markrodgers11

    Joined:
    Jul 10, 2013
    Posts:
    23
    Yes, I know that but I'm looking for an alternative method because that method will not work. The classname depends on what the player clicks in-game. Therefore I can't use "as Classname" unless I know the classname, which could be any number of things. I need someway to get find a script component and enable it by a string.

    Code (csharp):
    1.  
    2. if(inventory[index].equipable) {
    3.     GameObject instance = (GameObject)Instantiate(Resources.Load(inventory[index].item_name), transform.position + transform.forward + Vector3.up* 0.5f, transform.rotation);
    4.     instance.name = inventory[index].item_name;
    5.     //instance.GetComponent<BaseballBat>().enabled = true;
    6. }
    7.  
    Where BaseballBat I need inventory[index].item_name, because they aren't always picking up a bat, they could be picking up a hammer, etc.
     
    Last edited: Jul 31, 2013
  6. SchneiderAtvis

    SchneiderAtvis

    Joined:
    Feb 19, 2013
    Posts:
    38
    Inheritance can help you with this. Say your BaseballBat and Hammer inherits from Item, then you could do GetComponent<Item>().enabled = true;
     
  7. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    You should learn to read the API documentation, and try to resolve your problem by considering the error message.

    It tells you that Component does not contain a definition for enabled, yet you know that your item class (be it a baseball bat or hammer) does have this definition. It's being defined somewhere, so where is it defined?
     
  8. Mister-E2

    Mister-E2

    Joined:
    Jun 6, 2013
    Posts:
    179
    Don't all unity components have the enabled flag?
     
  9. markrodgers11

    markrodgers11

    Joined:
    Jul 10, 2013
    Posts:
    23
    Thank you for the only legitmate answer. I understood components don't have .enabled, thats not what I was asking. I was trying to find an alternative method to accomplish what I was trying to do, and this is what I ended up doing. I made a Weapon class that BaseballBat inherits. Thank you