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 TheGamingBurrito · Oct 05, 2014 at 03:09 PM · javascriptguiscorefindpoint

Point system help!

Hello, im creating a simple system, where if the player clicks on a gui button, the player`s gem count goes down, however its not working...

The gem count script:

 #pragma strict
  
 var Gems : int = 0;
 var healthGUI : GUIText;
  
 function Start () {
  
 }
  
 function Update () {
  
 OnGUI();
 
 }
  
 function OnGUI () {
  
 healthGUI.text = "Gems " +Gems.ToString(); 
  
 }

And this is the gui button script

     var btnTexture : Texture;
 
 function Start(){
 GemsCount = GameObject.Find("Camera").GetComponent(GemsCount);
 }
 
 
     function OnGUI() {
         if (!btnTexture) {
             Debug.LogError("Please assign a texture on the inspector");
             return;
         }
         if (GUI.Button(Rect(150,300,250,50)," ",""))
         
         GemsCount.Gems++;
 }

However i get this error: Expression GemsCount cannot be assigned to.

can anybody help?

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
1

Answer by JSierraAKAMC · Oct 05, 2014 at 04:08 PM

The problem is that you did not set up the variable at the beginning of the second script. The correct code should be something like this:

     var btnTexture : Texture;
     var gemCount : GemsCount;
      
     function Start(){
     gemCount = GameObject.Find("Camera").GetComponent(GemsCount);
     }
      
      
         function OnGUI() {
             if (!btnTexture) {
                 Debug.LogError("Please assign a texture on the inspector");
                 return;
             }
             if (GUI.Button(Rect(150,300,250,50)," ",""))
      
             gemCount.Gems++;
     }
 

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 Seneral · Oct 05, 2014 at 05:25 PM 0
Share

That, of course, too. I supposed this would be a pseudocode so I didn't mentioned it before:|

avatar image
0

Answer by Seneral · Oct 05, 2014 at 04:29 PM

You probably need to take a differnet name for GemsCount as it matches with the class name. Try "gemsCount".

Secondly, OnGUI() is called every frame by the engine, so theres no need to call it in the Update function.

Thirdly, I remind me that a GUIText only needs to be redefined on a change, means call

 gemsCount.healthGUI.text = "Gems: " + gemsCount.Gems; 

after adding the additional gem in the button script.

PS: Please note I'm not familar with JS but with C#, should probably work either way! :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can I reference a gameObject in a script attached to a different gameObject? 2 Answers

Score counter out of control, help needed. 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Pause When Score Reached? 2 Answers

Need help displaying score (JavaScript) 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