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 RescuerRus · Oct 12, 2013 at 02:20 PM · guitext

GuiText Problem

Sup, guys. I've written the next script:

 using UnityEngine;
     using System.Collections;
     
     public class Gold : MonoBehaviour {
         private KingController KingController;
         
         void Awake() {
         KingController = GetComponent<KingController>();    
         }
 //comment
         void Update () {
         string format = System.String.Format("Gold:", KingController.Gold);    
         guiText.text = format;
         
         }
     }

But Unity says that 12 lane cause this error: alt text I wanted to display int "Gold", taken from other script, but something goes wrong. Don't get it, what's wrong =( Help pls

screenshot.jpg (8.9 kB)
Comment
Add comment · Show 4
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 robertbu · Oct 12, 2013 at 02:54 PM 1
Share

Is the $$anonymous$$ingController script attached to this game object? GetComponent() won't find it if it is attached to some other game object.

avatar image RescuerRus · Oct 12, 2013 at 02:56 PM 0
Share

Ehm, what? GetComponent() uses to address properties of other scripts or components =) Or maybe i could be mistaken, but if so, how can i then get component from script on other object?

avatar image robertbu · Oct 12, 2013 at 03:04 PM 1
Share

You need a reference to the game object that contains the script. You can use GameObject.Find() to get the reference, or you can create a public variable and drag and drop the game object onto the variable in the inspector.

 GameObject go = GameObject.Find("GameObjectThatContains$$anonymous$$ingController");
 kingController = go.GetComponent<$$anonymous$$ingController>();

Also variables (by convention) should start with a lower case letter and never be named the same as the class.

avatar image RescuerRus · Oct 12, 2013 at 03:11 PM 0
Share

Fine, it works now, tvm <3

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FusionSausage · Oct 12, 2013 at 02:56 PM

I would say instead of using getComponent trash that row and in the KingController script add this line instead of your declaration:

 public int Gold { get{ return gold; } set{ gold=value; } }

Now you only need to type KingController.gold in your Gold class and it's better because you can both get and change (set) the variable in your Gold class.

And are you sure that you have spelled KingController right, in all ways (capital letters) and such? That was a problem I had.

Comment
Add comment · Show 2 · 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 RescuerRus · Oct 12, 2013 at 03:12 PM 0
Share

I've used robertu's way, but anyway thx for attention =)

avatar image FusionSausage · Oct 12, 2013 at 03:21 PM 0
Share

No problem, but remember that there is always this method that is very good for getting variables from other scripts :)

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Help applying ObjectLabel.js GUIText code to multiple objects 1 Answer

GUIText Alpha transparency not working on Android Table? 0 Answers

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers

How to I make a Death Counter? 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