Code (csharp): instance.GetComponent<TYPE>().enabled = true; how would I go about finding a component attached to the instance gameobject via a string? example: Code (csharp): instance.GetComponent<STRINGHERE>().enabled = true;
You mean like: Code (csharp): GetComponent("Typename"); https://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
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?
The problem it that you get (how supprising) a UnityEngine.Component form GetComponent("Typename"); You have to cast it so that it is the type of class / script you are aiming for. Code (csharp): (GetComponent("Classname") as Classname).enabled = true; http://answers.unity3d.com/questions/16737/why-cant-i-set-getcomponentsomecomponentenabled-fa.html
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): if(inventory[index].equipable) { GameObject instance = (GameObject)Instantiate(Resources.Load(inventory[index].item_name), transform.position + transform.forward + Vector3.up* 0.5f, transform.rotation); instance.name = inventory[index].item_name; //instance.GetComponent<BaseballBat>().enabled = true; } 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.
Inheritance can help you with this. Say your BaseballBat and Hammer inherits from Item, then you could do GetComponent<Item>().enabled = true;
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?
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