Using AddComponent to add a Sub Class using a String

I’m trying to temporarily add a component that is a sub class in order to load certain variables.

var equipTex = GetEquipment("Gloves");

Gloves inherits from a type “Equipment”.

public string GetEquipment(string equipmentName)
	{
		var equipName = (Equipment)gameObject.AddComponent(equipmentName);
		Debug.Log(equipName);
}

When I try the code above, I will get this:

Can’t add component because class
‘Gloves’ doesn’t exist!
UnityEngine.GameObject:AddComponent(String)

However, if I were to assign “equipmentName” to Equipment, it will not give me that error.

I’m beginning to believe that subclasses cannot be used by storing their name as a string, and calling them by that string, but classes that inherit from Monobehaviour can.

Is their something that I’m not getting? Or will I have to try another method?

OK here I got it working with a generic type:

void GetEquipment() where T : class, new(){
    gameObject.AddComponent(typeof(T));
}

and you call like this:

    GetEquipment<Gloves>();

It works for me, so there is no reason it should not work for you. :slight_smile:

Edit:
Let’s take an example. One file:

using UnityEngine;
using System.Collections;

public class Equipment : MonoBehaviour {

	public Vector3 velocity;	
	public Transform _transform;	
	protected int health;

	public virtual void Start(){
		velocity = new Vector3(Random.Range(0.5f,1.5f),0,0);
		_transform = GetComponent<Transform>();
	}
    public virtual void Update(){
     	//Some Update
    }
}

public class Gloves : Equipment {
	public int variable;
	public override void Start () {
		base.Start();
		variable =10;
	}
	
	public override void Update () {
		base.Update ();
	}
}

Ok so we have in the same file two classes Equipment and Gloves that inherits from Equipment

Just for the example here is a third independent class:

using UnityEngine;
using System.Collections;

public class NPC : Enemy {
	public int varToShowInTheInpsector;
	public override void Start(){
		base.Start ();
        varToShowInTheInpsector = 100;
	}

	public override void Update(){
               //Some stuff happening here
	}
}

Now we have an object with another script that is not any of those above:

using UnityEngine;
using System.Collections;

public class MyClass : MainClass {

    public override void Start () {
        base.Start ();
     }

    public override void Update () {
       //Many stuff here
      if(Input.GetKeyDown(KeyCode.A)) 
         GetEquipment<Gloves>();
      else if(Input.GetKeyDown(KeyCode.B)) 
         GetEquipment<NPC>();
      else if(Input.GetKeyDown(KeyCode.C)) 
         GetEquipment<Equipment>();
    }
    void GetEquipment() where T : class, new(){
        gameObject.AddComponent(typeof(T));
    }
}

Try that on and you will see new script popping up on your inspector. I added some public variable so that you can see that the appropriate class is created. You see how easy it gets to add any custom component?

I tested it, and your problem is that Gloves is in the same file as Equipment. Create a Gloves.cs. It would have worked with AddComponent( typeof(Gloves) ) though.

For the moment, to get it in a working condition, I started writing functions for each Equipment, and I’m using

SendMessage(equipmentName); 

to call the function with the appropriate name of the Equipment I want. That function will load the values I need, and presto. Now that I think about it, writing a class for each equipment would have been a little overboard, since I only need the classes to hold variables XD

What are you guy’s opinion on this?

Your problem is, this is half C#, half JavaScript!!! Otherwise though, what you are trying to do will work perfectly, if you just get the SYNTAX right:

case JavaScript:

function GetEquipment(equipmentName : String)
    {
       var equipName = (Equipment)gameObject.AddComponent(equipmentName);
       Debug.Log(equipName);
       return equipName.ToString();  //????
}

var equipTex = GetEquipment("Gloves");

in JAVASCRIPT, the class and subclass are declared this way:

[Does Unity support Inheritance and Subclassing? - Questions & Answers - Unity Discussions][1]

case C#:

public string GetEquipment(string equipmentName)
    {
       Equipment equipName = (Equipment)gameObject.AddComponent(equipmentName);
       Debug.Log(equipName);
       return (string)equipName;   // ???
}

string equipTex = GetEquipment("Gloves");

the classes here are declared this way:

public class Equipment : MonoBehaviour
{

…etc

public class Gloves : Equipment
{

…etc
[1]: Does Unity support Inheritance and Subclassing? - Questions & Answers - Unity Discussions