Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DryTear · May 31, 2013 at 08:49 PM · stringtextfieldgameobject.find

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)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julien-Lynge · May 31, 2013 at 09:12 PM 0
Share

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?

avatar image DryTear · May 31, 2013 at 09:21 PM 0
Share

line 57 is GameObject.Find(Text).GetComponent(Text).enabled = true;

and no, i used Text just as a dummy var

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DryTear · May 31, 2013 at 10:08 PM 0
Share

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

avatar image DryTear · May 31, 2013 at 10:25 PM 0
Share

and both name of object and component are same

avatar image Julien-Lynge · Jun 01, 2013 at 01:04 AM 0
Share

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.

avatar image DryTear · Jun 01, 2013 at 04:37 AM 0
Share

both are correct, still wont find

avatar image Julien-Lynge · Jun 01, 2013 at 05:18 AM 0
Share

So you added debug statements? Can you post them here so I can see them too?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Clones not being Destroyed 1 Answer

GameObject.Find object with Variable string C# 0 Answers

How I can make a string equal to a textfield? 1 Answer

Display additional text before the variable that the user is editing in a GUI Text Field 1 Answer

TextField Query 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges