- Home /
How would I make the GetComponent field a variable?
What I'm trying to do is this,
using UnityEngine;
using System.Collections;
public class CheckBool : MonoBehaviour {
public string scriptName;
public string varName;
public bool Check () {
Component other;
other = gameObject.GetComponent(scriptName) as Component;
//This is the issue here. I want to get the variable on other using the name
//stored in the varName variable.
if (other.varName == true) {
Debug.Log ("Welp, it was true");
return true;
}
Debug.Log ("Sorry, it was false");
return false;
}
}
I want to access varName on other. But varName is a variable on the script. How would I do this?
Thanks.
Answer by Ashkan_gc · Aug 26, 2012 at 05:37 PM
You should use reflection for it.
If you want to do it without reflection, then you should do it the slow way, write functions like SetX(int i){X=i;} and call them using sendMessage and X is your variable name. You should execute more code and write more functions for your code to work and you can not use all datatypes but if reflection causes problems on iOS or you are lazy enough to don't learn it, then go this way.
Your answer
Follow this Question
Related Questions
AddComponent with parameter variable 2 Answers
Passing a Script Name to a Function 2 Answers
How could you access a script of varying name? 5 Answers