—Skip to the underlined solution section for the answer.—
Less of a “how to” and more of a “how I”
Caveat: I’m new to coding so sorry if the terminology is wrong or if the solution is bad.
-this doesn’t actually let you assign the type, but it is the exact work around for a generic type that I have been looking for.
The problem is: I have a class that looks for a certain component to know if it is the type that it needs. I want to reuse this code for other items, but I don’t want it to look for the same component every time. I don’t want to re-write the code for every type. I also don’t want to make a new class every time I want to change what it is looking for.
Here is the solution that I came to:
- Make a list of type - other types may work (eg; )
- Have prefabs with the target behavior on them, assign them to this list in the inspector
- Use this version of try get component: TryGetComponent(Type type, out Component component)
Here is how I used it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class RunAwayAction : MonsterBaseAction, IInteractable
{
[SerializeField] private List<MonoBehaviour> _thingsToBeAfraidOf;
private Transform _afraidOf;
public void Interact(GameObject heldItem)
{
if (heldItem == null) { return; }
for (int i =0; i < _thingsToBeAfraidOf.Count; i++)
{
if (heldItem.TryGetComponent(_thingsToBeAfraidOf[i].GetType(), out Component objectOfFear))
{
_afraidOf = heldItem.transform;
break;
}
}
}
}
For some reason GetType() gives the most specific class even if the classes is taken from a list of monobehaviors. for example, I have a script called “burnableObject” even though it inherits from Monobehavior, the GetType() function will return burnableObject when it is taken from this list in this way, which is perfect and exactly what I needed to make this work.
Hope this Helps!