Reputation: 13
I'm using the latest recommended version of Unity and I'm trying to access an array of strings localized in a method in a different script. I want to be able to access the method by using the GetMethod function in the System.Reflection library. There aren't any compiler errors in my script but whenever I try to test the game, Unity freezes. This is what I have so far:
preMaps = GetComponent<PreMaps>();
MethodInfo Method = preMaps.GetType().GetMethod(mapName);
Method.Invoke(preMaps, null);
foreach (LocalVariableInfo variable in Method.GetMethodBody().LocalVariables)
{
print(variable);
}
I simply put a print line to test if I was getting any variables out of the method and I previously declared the preMaps variable as a PreMaps class at the beginning of the script. The mapName variable is a string that matches the name of a method in the other script (previously declared and initialized). Is there anything that I'm missing? I've tested it without these lines and Unity doesn't freeze.
using UnityEngine;
public class PreMaps : MonoBehaviour
{
public class MapName
{
public readonly static string[] map = {
"p,250,110,50,184,10",
"start,10",
"n,0,0,0,100",
"fin"
};
}
}
This is all that's in the other script I'm trying to access.
Upvotes: 0
Views: 903
Reputation: 90813
PreMaps.MapName
is a CLASS (Type), not a Method!
And PreMaps.MapName.map
is a FIELD within that class, not a LocalVariable!
Anyway I see no need for / sense in using Reflection at all!
All your types and their fields are public
anyway so as far as I can tell there is absolutely nothing that hinders you from simply doing
foreach (var mapValue in PreMaps.MapName.map)
{
Debug.Log(mapValue);
}
Upvotes: 1