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 SGamerXxX · Aug 06, 2012 at 02:01 AM · guitriggertextadd

Help me with Gui Text

OK so here's the thing, I have some gameObjects when collided with the player equal to 10 points and the other 20.

But the problem is that can't get it to update/add the score.

It will do the 10 points but when I hit the 20 it does not do anything to the Gui text( though it does debug...)

Heres the code can someone help me figure out what is wrong?

P.S only difference between 10 point and 20 Point code are the points themselves.

 var label : GUIText;
 var Counter ="";
 var Score : String = "Score:";
 
 
 function Update() {
     label.text = Counter;
     
 }
 
 function OnTriggerEnter(){
 
 Counter = Score +=10 ;
 Debug.Log("Get 10 Points!");
  
  
 }
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 SGamerXxX · Aug 05, 2012 at 11:35 PM 0
Share

Sorry if i stuffed up the code part i could not get it working properly

avatar image Seth-Bergman · Aug 06, 2012 at 02:32 AM 0
Share

at least you tried :)

2 Replies

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

Answer by Seth-Bergman · Aug 06, 2012 at 02:41 AM

for starters,Score += 10 isn't right..

say :

Score + 10;

but this will just print "Score:10" every time, if you want to accumulate points, you need an int to store the current score:

 var label : GUIText;
 var Score : String = "Score:";
 var currentScore : int;
 
 
 function Update() {
     label.text = Score + currentScore;
 
 }
 
 function OnTriggerEnter(){
 
 currentScore += 10;
 Counter = Score + currentScore ;
 Debug.Log("Get 10 Points!");
 
 
 }

of course, each object with this script has it's own instance of currentScore, so whichever one hits will have its own unique score.. if you want a unified score, you would access a static var:

on player script (named "PlayerScript"):

 var label : GUIText;
 var Counter ="";
 var Score : String = "Score:";
 static var currentScore : int;
 
 function Update() {
     label.text = Counter;
 
 }

on trigger script:

 function OnTriggerEnter(){
 
 PlayerScript.currentScore += 10 ;  // use name of script
 Debug.Log("Get 10 Points!");
 
 
 }

hope this helps EDIT: ok, let me try to clarify:

first off, my mistake, I had put the "static" on the wrong line, it's fixed now; the point is, you need to declare an int (or float) to store numeric data, rather than a string; by declaring the int as static, we are saying this value is universal through all instances of this script. So, if I change it in one place, it changes everywhere. Of course, this won't matter much if the script in question is only used once, and normally it would be just once, on the player for example, which is why I did it that way.. BUT, just declaring it right in the same script would still keep it constant (if static) either way..

 var label : GUIText;
 var Counter : String;
 var Score : String = "Score:";
 static var currentScore : int;
 var myDamage : int = 10;
 
 function Update() {
     label.text = Counter;    
 }
 
 function OnTriggerEnter(){    
 currentScore += myDamage;
 Counter = Score + currentScore ;
 Debug.Log("Get " + myDamage+" Points!");   
 }

here the score is instanced in each object, but the overall score stays static between all objects (if I add 10, it updates every object to the same value)

which is basically what you want I think, just set the var myDamage to 20 on the other object prefab only prob is for performance, there's really no need to be updating the GUIText from EVERY object with this script(though I think it should still work though)..

Better to have one script somewhere drawing the GUIText, and access the int from the script with multiple instances, so in one script on ONE gameobject in the scene:

 var label : GUIText;
 var Score : String = "Score:";
 static var currentScore : int;
 
 function Update() {
     label.text = Score + currentScore;
 
 }

now since currentScore is static, we don't even need an instance of the script to access it, we can just refer to it directly using the name of the script:

 var myDamage : int = 10;
     
 function OnTriggerEnter(){    
 OtherScriptName.currentScore += myDamage;
 Debug.Log("Get " + myDamage+" Points!");   
 }

this should work as seen here :)

Comment
Add comment · Show 4 · 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 SGamerXxX · Aug 06, 2012 at 04:39 AM 0
Share

Im afraid this is confusing me more, i tried the first script box and seem to do much the same as my script was doing

The are second and third script box are they part of the first or separate. the "player script" is it supposed be attached to the player gameObject or something else entirely. I'm guessing the "trigger script" is any gameObject that want to give points.

But when I tried the first script original i had to take out change the "static var" to a normal one or else i got this really weird error "BCE0135: Invalid name:script name".

Please write again, Still confused but also understand it a bit better...

avatar image Seth-Bergman · Aug 06, 2012 at 05:09 AM 0
Share

ok, edited my answer, hope that clarifies..

sorry had static on the wrong line before, my mistake

edit to the edit: just re-edited again to be even clearer, the example there SHOULD work now, I think

avatar image SGamerXxX · Aug 06, 2012 at 08:40 PM 0
Share

Almost but not quite... Im still get the error BCE0135: Invalid name: '10 Pointer'

10 Pointer being the script name, and error is occurring on both this line: "currentScore += myPoints;" and this line... "Counter = Score + currentScore;"

It really is hating on that static var.... (I changed the myDamage to myPoints)

avatar image SGamerXxX · Aug 07, 2012 at 12:19 AM 0
Share

Working now thanks a lot.

avatar image
0

Answer by DNP · Aug 06, 2012 at 04:46 AM

 var gText1: GUIText;
 var gameHolder : GameObject;
 
 function Update(){
 gText1.text = gameHolder.GetComponent(NewerGameScript).totalNotebooks.ToString();
 }


This is a shortened part of the code i use for my game. And, Yes it works. There should be no problems with this.

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

9 People are following this question.

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

Related Questions

Trigger collision with Player won't work 2 Answers

Help with making a triggered gui message 1 Answer

Display a Text/GUI on screen when triggerd with Fadein/Fadeout 1 Answer

[4.6 JS] How to show GUI text on a trigger enter and then take it away on exit? 1 Answer

Using a trigger to make a scrollview appear and then disappear? 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