Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Er_ellison · Aug 01, 2017 at 08:26 PM · selecting objects

Selecting a component of gameObject

Hi , I AM ROOKIE In unity please help me first of all i should say that i searched but i cant find my answer so i have a empty game object that i assigned the script in it: here is the script using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Gear_setup : MonoBehaviour {
 
     public GameObject gear;
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         Text Gear = GetComponent ("Text").text;
         Debug.Log (Gear);
     }
 }
 


and i dragged and dropped the other game object on public GameObject gear; section on that code the problem is the game object that i assigned to gear has a text and i want to Debug.log("THE TEXT"); how can i do that ?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by ShadyProductions · Aug 01, 2017 at 08:31 PM

Never use GetComponent in update, as this is an expensive call. And you don't want to call it every frame.

Instead cache it inside a variable in the class scope.

  public class Gear_setup : MonoBehaviour {
 
  public GameObject Gear;
  private Text _textComponent;
  // Use this for initialization
  void Start () {
      _textComponent = Gear.GetComponent<Text>();
     Debug.Log(_textComponent.text);
  }
  }
Comment
Add comment · Show 3 · 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 Er_ellison · Aug 01, 2017 at 09:20 PM 0
Share

the gear text is changing evert time that the car moving so
i need to know that every frame, so that is why i put the Debug part in update
in u'r solution the gear is been shown once , but i want to know the value of that every frame
what can i do ?

avatar image ShadyProductions Er_ellison · Aug 02, 2017 at 06:49 AM 0
Share

Then put the debug in the update, but never use GetComponent in update. Like:

   public class Gear_setup : $$anonymous$$onoBehaviour {
  
   public GameObject Gear;
   private Text _textComponent;
   // Use this for initialization
   void Start () {
       _textComponent = Gear.GetComponent<Text>();
   }
   void Update() {
      Debug.Log(_textComponent.text);
   }
   }
avatar image Er_ellison · Aug 02, 2017 at 07:52 AM 0
Share

thanx every one that contributed !

avatar image
0

Answer by look001 · Aug 01, 2017 at 08:42 PM

I'm not completely sure what you mean. As i understand you have a GameObject "gear" with a Textcomponent applied to it. To show the text in the console you need to change a bit.

 void Start () {
     string text = gear.GetComponent<Text> ().text;
     Debug.Log (text);
 }

Also you need to import "UnityEngine.UI" to use the Text class. For that you add the line using UnityEngine.UI; to the top. Btw. it's recommend to write variable names with a capital letter. Methods and Classes start with a Capital letter. Good Luck!

Comment
Add comment · 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

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

68 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot set Selection.activeObject (no errors, no clues) 0 Answers

Selecting objects ingame and rotating it 2 Answers

Mobile select and move Object with offset 0 Answers

Select object in scene problem 3 Answers

Selecting objects in arbitrary 2d space using only arrow keys 2 Answers


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