Issue referencing a method that exists in a component that is only decided at runtime
I'm in the process of building a game, and a specific issue is occurring when trying to access a method of a script/class. The primary issue is that exactly which script/class is going to be used will not be decided until runtime, as it will vary depending on what options a user has selected. For example, the component may be named 'Attract', 'Repel', 'Emit', 'Throw', etc. Despite this, all of them contain the same two methods, slightly modified for each of their particular setups - 'ActionSetup()' and 'GetTriggerObjTag()'.
The contents of these methods appears to be irrelevant, as my issue is I get the following error when trying to compile:
Type 'UnityEngine.Component' does not contain a definition for 'ActionSetup' and no extension method 'ActionSetup' of type `UnityEngine.Component' could be found. Are you missing an assembly reference?
I understand why this is happening, because GetComponent() isn't directly defined - it is using a variable in the place of a specific component. My question is, how do you get around this issue when you don't know the name of the component until runtime? The code of the method that calls GetComponent() in this way is below:
void ApplyToAll(GameObject C1, GameObject C2, GameObject Act)
{
// create array filled with objects matching tag of C1 (what it is being applied to)
GameObject[] ObjForScript = GameObject.FindGameObjectsWithTag (C1.tag);
// run through every object in array and...
foreach (GameObject obj in ObjForScript)
{
// check if script is already attached. If not, add it.
MonoBehaviour testObj = obj.GetComponent (Act.name) as MonoBehaviour;
if (testObj == null) {
AddCorrectAction (obj, Act);
obj.GetComponent(Act.name).ActionSetup (C2);
}
// if it is attached, check if the trigger is the same as what will be added.
// if it different, add script, otherwise do nothing
else if (testObj != null) {
if (testObj.GetTriggerObjTag () != C2.tag) {
AddCorrectAction (obj, Act);
obj.GetComponent (Act.name).ActionSetup (C2);
}
else {
break;
}
}
}
}
Any help would be greatly appreciated. Thankyou in advance.
Answer by Bilelmnasser · Apr 13, 2017 at 10:06 AM
public class componentisDecidedRuntime:MonoBehaviour
{
//keep reference of component in static variable
public static componentisDecidedRuntime Instance;
void Start()
{
Instance=this;
}
public void foo()
{
}
}
//to access foo;
if(componentisDecidedRuntime.Instance!=null)
componentisDecidedRuntime.Instance.foo();
Thankyou very much for posting.
I ended up having to manually write a few methods to handle each type of component I might need, but knowing that this is possible and how to do it will no doubt make some parts easier in the future development of my game. Thankyou for you help
Your answer
Follow this Question
Related Questions
Get Owner Of Bounds On Intersect 1 Answer
Difference between GetComponent 2 Answers
Activating Scripts in another Script? 2 Answers
Unity GetComponent Script? 1 Answer
How to convert string to runnable code? 0 Answers