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 Iceo MK · Nov 11, 2013 at 05:16 AM ·

Adding score to GUI Text via another script/variable.

I'm a little confused as to how the GetComponent works in terms of accessing a variable and changing its' value.

What I need is seemingly quite simple, the idea is the following.

I have a GUI text called "GUiScore". It has a "Text" field which I want to be able to access from another script. So far I made it so that this GUiScore is linked to a reference script and it works if I code something within that script (Script A). Script A also has a public variable, which is the GUIText variable.

Now on another script (which is a system I made to count score) (Script B), I want to send the information of a variable within Script B, into that variable called GUIText from Script A. How can I access GUIText variable from Script A and change its' value/text?

I don't know the code for it... I tried things like: GetComponnent(ScriptA.referenceVariable) = scoreVariable; and so many other weird things that I'm sure aren't correct, because I keep getting errors.

Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by mattssonon · Nov 11, 2013 at 08:27 AM

First you need to find Script A. You can either do this by setting it as a public variable on Script B, like you did with the GUIText on Script A, or you can find its GameObject and then the script by first using the built-in Find() method, e.g.:

 GameObject scriptAGameObject = GameObject.Find("Name of Game Object which has Script A");

So, now you have Script A's Game Object, and now you need to find the Script A component on the GameObject, like this:

 ScriptA scriptA = scriptAGameObject.GetComponent(ScriptA);

Now you have a reference to Script A, and you want to change a GUIText attached to this script. You do this by accessing the name of the attached GUIText (I assume that's what you were trying to do in your question):

 scriptA.referenceVariable = scoreVariable;
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 Iceo MK · Nov 11, 2013 at 04:48 PM 0
Share

I didn't know how to properly utilise the "Find()" method you showed, nor did I know how to do the first suggestion being to make Script A as a public variable within Script B. I don't know how to assign it, for example:

 public var ScriptA : String;

Is the above correct?

Either way, I simply placed Script B onto the GUI Text object, and declared the variable within that one script.

Thanks for your help.

avatar image mattssonon · Nov 11, 2013 at 07:33 PM 0
Share

All right, maybe you should take a look at some basic Unity tutorials then, e.g. here: http://unity3d.com/learn/tutorials/modules

However, you would assing it like this:

 public var myScript : ScriptA;

Then you can use myScript to access stuff in ScriptA. Either way, if your question is solved please accept an answer or close the question.

avatar image
0

Answer by thef1chesser · Nov 11, 2013 at 08:39 AM

If I understand this correctly the textfield is in the GUI and when you access it, it should already be shown on screen starting with probably a score of 0.

in ScriptA I would make the public variable static.

 public static GUIText GUITextVariable = 0;

so in ScriptB I would have

 ScriptA.GUITextVariable = score;
Comment
Add comment · Show 6 · 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 hamstar · Nov 11, 2013 at 08:48 AM 1
Share

I'm not sure I agree with this because then you would not be able to use ScriptA on multiple game objects. I know it might be unlikely to use a score script in two places, but it depends what's in the script. (thinking split screen, mutliplayer..)

avatar image mattssonon · Nov 11, 2013 at 08:54 AM 0
Share

I agree with @hamstar, you should not make a variable static just to make it easier to access, that's not why you use the static declaration.

avatar image thef1chesser · Nov 11, 2013 at 09:16 AM 0
Share

Score can only be used once so it might as well be some kind of singleton.

For multiplayer use Score1, Score2, etc.

I know static should not be used lightly. But this would be a case where I would use it and not for the easy to access part.

@matt don't make assumptions that easily.

avatar image mattssonon · Nov 11, 2013 at 09:20 AM 0
Share

There's no reason not to re-use such a script several places.

avatar image thef1chesser · Nov 11, 2013 at 09:37 AM 0
Share

In a single player game I'd like to have my score variable as a singleton.

For multiplayer, as I mentioned, Score1, Score2, etc. comes to $$anonymous$$d as a solution there.

Everyone is free to agree or disagree to this solution.

I agree to the fact that this solution only works in specific cases and that people should not use static just because it's easy to access.

Show more comments
avatar image
0

Answer by Dracorat · Nov 11, 2013 at 07:52 PM

A game should be able to execute without a gui at all - that is, all of the logic and variables that control game execution should have zero awareness of a GUI.

The smallest exception to this are actions that are fired from a GUI of course, but even then, the actions should be called from GUI, but not need data from the calling.

If you can write your game like this, it means that each object that "owns" some piece of data, like the health of a player, the ammo reserve in a weapon, the name of a level, etc, will expose some method to retrieve that information and will update that information in its own container.

Using ammo as an example, it means that there (likely) be a "Weapon" class that contains information about the weapon, along with something like:

 public int GetCurrentAmmo(){
     return this.ammoCount;
 }

Then, in the GUI, all you do is when it's time to display the ammo, you call the appropriate method, such as: Player.CurrentWeapon.GetCurrentAmmo() and put the result on the GUI as appropriate.

A GUI is a "peek" in to variables belonging to the runtime execution of the game. No part of the game should be modifying the GUI directly unless that part of your game is part of the GUI. That means if you want to say - pause the game from input, you should change some state to "Paused" and the GUI should be able to see that state and show the appropriate paused gui.

By maintaining this level of segregation, you will greatly ease your debugging and be able to more easily change GUI elements as your game matures and you realize changes in scope of your game.

Comment
Add comment · Show 1 · 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 Iceo MK · Nov 11, 2013 at 08:01 PM 0
Share

Thank you for the heads up, I didn't know about this before hand.

I'm new into the program$$anonymous$$g field, and want to make my own indie-game via Unity. This kind of information is great, and I suppose it'll take much longer than I thought (with my little to no skills).

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

20 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

Related Questions

Public List of Scripts? 1 Answer

Animation clips anyone? 1 Answer

How do you add levels to your game? 1 Answer

Need Help for AirPlane Script 1 Answer

script compilation error 0 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