- Home /
GameObject.Find(TextFieldVariable) - Doesnt seem to work!
Heres my problem: I have a GUI.TextField in my script, and im trying to use the TextField's value in GameObject.Find(); like here:
var Text : String = "Thing";
function Update()
{
if(Input.GetKey("o"))
{
GameObject.Find(Text).GetComponent(Text).enabled = true;
}
else
{
GameObject.Find(Text).GetComponent(Text).enabled = false;
}
}
it supposed to look for Text, which in this case is:
Text = GUI.TextField(Rect(5,5,200,30), Text, 30);
and this is what the log yells at me:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value) Script.Update () (at Assets/Scripts/Script.js:57)
So you're using Text for both a string and a type of component?
What line is your error referring to - what is at line 57?
line 57 is GameObject.Find(Text).GetComponent(Text).enabled = true;
and no, i used Text just as a dummy var
Answer by Julien-Lynge · May 31, 2013 at 09:37 PM
What you have there won't work, unless you happen to have a GameObject and a component with the same name and that name matches the string you're testing against.
If Find(Text) does not find something, it will return null. If it finds something, but that something doesn't have a component of type Text, that will return null. In either case, you'll then have a null reference that you're trying to set enabled, and that will throw an error.
well yah, as i said Text is just a dummy var, in my case im doing this:
GameObject.Find(slotItem1).GetComponent(slotItem1).enabled = true;
when slotItem1 is a String that can be edited with a TextField in game, but whenever i press 1(use slotItem1, where it finds it and enables it), the error pops up
So as I said, your error is either that the Find function isn't finding the GameObject, or the GetComponent isn't finding the component. Add some debug statements, figure out which it is, figure out why, and fix it.
So you added debug statements? Can you post them here so I can see them too?