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 Takzdeveloper · Aug 07, 2014 at 12:16 PM · guicolliderscoresendmessage

Trouble displaying a variable as a GUI from a different script

Hi all,

I'm trying to display the data in the variable "playerScore" from the script "ScoringAndEnemyDestroy". I want the data from "playerScore" to be placed in the variable "totalScore" in the script "ScoreGUI" so I can display that data as a GUI. The GUI cannot be created in the "ScoringAndEnemyDestroy" script as the GUI repeats itself multiple times as the script is used on multiple colliders.

 public class ScoringAndEnemyDestroy : MonoBehaviour {
 
     public long playerScore;
 
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     public void Update () {
         PlayerScore ();
     }
 
     public void OnTriggerEnter(Collider other){
         Debug.Log ("Entering collider");
 
         if (other.gameObject.tag == "Enemy") {
                 Destroy (other.gameObject);
                 playerScore += 5;
 
         if  (other.gameObject.tag == "Boss") 
                 Destroy (other.gameObject);
                 playerScore += 10;    
 
         
         }
     }
 
     void PlayerScore()
     {
         SendMessage ("TotalScore", playerScore, SendMessageOptions.DontRequireReceiver);
 
     }
 
 
 }

 public class ScoreGUI : MonoBehaviour {
 
     public long totalScore;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void TotalScore (long playerScore)
     {
         Debug.Log("PlayerScore: " +playerScore);
         if (playerScore > 0)
                         playerScore = totalScore;
         Debug.Log ("Total score is being calculated: " + totalScore);
     }
 
     void OnGUI() {
         
         GUI.TextArea (new Rect (25,25,50,50), totalScore.ToString());
         
     }
 }

All help is greatly appreciated,

Many thanks

Comment
Add comment · Show 1
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 Takzdeveloper · Aug 07, 2014 at 01:32 PM 0
Share

Can anyone help? Been stuck on this for an irritating amount of time

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Aug 07, 2014 at 12:19 PM

SendMessage only broadcasts to the monobehavior scripts in this gameobject(where it was sent from), however BroadcastMessage will do the same, but all children as well. I'm not sure if either of these will suit your needs. You may need to get a reference to the gameobject that ScoreGUI is attached to and use that reference to update the field totalScore. If you can do this instead, you find it's better in the long run.

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 Takzdeveloper · Aug 07, 2014 at 12:25 PM 0
Share

The thing is, the Debug.Log in ScoreGUI is displaying the "playerScore" variable just fine in the console. And ScoreGUI and "playerScore" are attached to different objects. I just need a way of displaying that same variable within a GUI on the ScoreGUI script, but obviously the send message is only sending to the TotalScore function and not the whole ScoreGUI script.Are you suggesting to use GetComponent? ScoreGUI is attached to an object called DefencePoint.

avatar image Landern · Aug 07, 2014 at 01:35 PM 0
Share

Yes to GetComponent once your find the gameobject called "DefencePoint", access the componenet and keep a reference to that(stuff it into a variable of the same type(ScoreGUI) and use that reference for updates and getting the latest score. There really should only be one type keeping score, so i hope ScoreGUI isn't' attached to gameobjects all over the place, that would be horrible.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Adding 1 point after collison 1 Answer

How to make an object enter a gui screen upon a collision? 1 Answer

Why is OnGUI not working? 1 Answer

Adding score when enemy dies (Errors) 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