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 Matt 5 · Jan 10, 2012 at 06:22 PM · guicounterpoints

Adding score to existing code

I would like to add a simple score counter to my code my issue is I have tried all other methods of doing this and it just doesn't seem to want to work .

Can anyone help me?

Here is the code I want to add the score counter to:

using UnityEngine; using System.Collections;

public class DamagableObject : MonoBehaviour {

 public bool m_Invincible = false;
 public float m_MaxHealth = 100f;
 
 [SerializeField]
 public float m_Health = 100f;
 
 
 public DecayingObject m_DeathEffectPrefab;
 public DecayingObject m_RemainsPrefab;
 public string m_DeathAnimation;
 
 /*public virtual float health
 {
     set
     {
         if (!m_Invincible ||  m_Health < value)
         {
             m_Health = value;
         }
         if (m_Health <= 0)
         {
             Death();
         }
         else if (m_Health > m_MaxHealth)
         {
             m_Health = m_MaxHealth;
         }
     }
     
     get
     {
         return m_Health;
     }
 }*/
 
 public virtual void DealDamage(float val, Vector3 pos, Vector3 normal)
 {
     if (val > 0f && !m_Invincible)
     {
         m_Health -= val;
         if (m_Health <= 0)
         {
             Death();
         }
     }
 }
 
     
 
 public virtual void Heal(float val)
 {
     if (val > 0f)
     {
         m_Health += val;
         if (m_Health > m_MaxHealth)
         {
             m_Health = m_MaxHealth;
         }
     }
 }
 
 public float GetHealth()
 {
     return m_Health;
 }
 
 protected void Start() {
     bool error = false;
     if (m_MaxHealth <= 0)
     {
         Debug.LogError("Max health can't be less than or equal to 0");
         error = true;
     }
     if (m_Health > m_MaxHealth || m_Health <= 0)
     {
         Debug.LogError("Invalid health range expected 0-" + m_MaxHealth + " got " + m_Health);
         error = true;
     }
     if (error)
     {
         Debug.Break();
     }
 }    
 
 
 
 protected void Death()
 {
     if (m_DeathEffectPrefab != null)
     {
         Instantiate(m_DeathEffectPrefab, transform.position, transform.rotation);
     }
     if (animation != null && animation[m_DeathAnimation] != null)
     {
         animation.CrossFade(m_DeathAnimation, 0.5f, PlayMode.StopAll);
         if (m_RemainsPrefab != null)
         {
             StartCoroutine("WaitForDeathAnimation", animation[m_DeathAnimation].length);
         }
     }
     else
     {
         if (m_RemainsPrefab != null)
         {
             Instantiate(m_RemainsPrefab, transform.position, transform.rotation);
         }
         Destroy(gameObject);
     }
 }
 
 protected IEnumerator WaitForDeathAnimation(float time)
 {
     yield return new WaitForSeconds(time);
     Instantiate(m_RemainsPrefab, transform.position, transform.rotation);
     Destroy(gameObject);
 }
 public Texture2D background = null;
 public Texture2D healthbar = null;
 
 
 void OnGUI()
 {
     //Draw Background
     
     GUI.DrawTexture(new Rect(25.0f,25.0f,120.0f,14.0f),background);
     
     //Draw Healthbar
     GUI.BeginGroup(new Rect(30.0f,30.0f,115.0f *(m_MaxHealth / m_Health),15.0f));
     GUI.DrawTexture(new Rect(0.0f,0.0f,120.0f,10.0f),healthbar);
     GUI.EndGroup();
     
        
                    
 }
     
 void Update()
 {                        
     
 }

}

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
Best Answer

Answer by roamcel · Jan 10, 2012 at 09:46 PM

Given that you don't specify HOW you intend to actually create your score feedback, I can only tell you that you can easily do that in

OnGUI

for example with a very very simple

GUILayout.Label("My score " + score);

where score is your score variable of course, set and updated elsewhere, naturally.

To acually hold the score information, you usually need to keep a 'game master' object in your scene, that basically has a function you call whenever score is earned.

Additionally, a more versatile but slightly harder approach is to use what are called 'singletons', which correspond to static classes in c#. Those classes have project wide, one instance variables, that can be dereferenced from anywhere on the project.

Supposing you have a static class like this:

using UnityEngine; using System.Collections;

public static class mygamemaster { //note how it doesn't derive from monobehavior public static int myscore = 0; public static string myplayername = "Player"; }

in any of your code you can just write

mygamemaster.myscore = 5;

and it'll work as intended.

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 Matt 5 · Jan 11, 2012 at 03:29 PM 0
Share

Thank you I will give that a shot and see what comes out from it

avatar image Matt 5 · Jan 11, 2012 at 05:41 PM 0
Share

I tried this and it works for one enemy and I am trying to Update it but it is just not working. I've tried doing Scoring.m_score ++;

but that just updates it all the time and I just want it to update when the death function is called again so I tried:

Update() { if(Death()) { Scoring.m_score += 1; } }

If I am doing something wrong can you or someone help me with this?

avatar image
0

Answer by Matt 5 · Jan 10, 2012 at 09:52 PM

What I was looking for and sorry for not explaining it in more detail was the score to display based on the the destroy of the gameObject and increase by 10 as each enemy is destroyed.

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 roamcel · Jan 11, 2012 at 06:44 AM 0
Share

you usually add your notes as comments rather than replies, since these last ones from your part make the question harder to understand. I've updated my reply with more info on the subject.

avatar image
0

Answer by roamcel · Jan 12, 2012 at 06:47 AM

Need code formatting for this so I'm making it a new reply (note: UNTESTED CODE): Your

 if(Death()) { Scoring.m_score += 1; } 

it's the right approach but it's a bit flawed. What you need is a FUNCTION to call in your scorekeeping script.

 using UnityEngine;
 using System.Collections;
 
 public class mygamemaster : MonoBehavior {
   public static int myscore = 0;
   public static string myplayername = "Player";
 
   public void increasepoints(int increase) {
     myscore += increase;
   }
 
   void OnGUI() {
     GUILayout.Label(myplayername + " score " + myscore);
   }
 }

then, whenever you need to increase your score, from another class

 GameObject gamemasterobject;
 gamemasterobject = GameObject.Find("gamemaster") //you need a gameobject called gamemaster in your scene for this to work, with the mygamemaster class attached
 gamemasterclass = gamemasterobject .GetComponent<mygamemaster>();
 gamemasterclass.increasepoints(1);
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 Matt 5 · Jan 25, 2012 at 05:33 PM 0
Share

This code works amazing but now I want to apply a texture that the score will display on now I've tried GUI.DrawTexture(Rect 100, 25, 100, 30), "texture name");

but that keeps co$$anonymous$$g up with errors, I've also tried GUI.DrawTexture(new Rect(100, 25, 100, 30),Scoring.m_score.ToString(),"texture name"); as well as

GUI.Label(new Rect(100, 25, 100, 30),Scoring.m_score.ToString(), "texture name");

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with basic magic system 0 Answers

Impact Force Gui 0 Answers

Make a custom score counter in unity with c# 1 Answer

Score Counter Help 2 Answers

Scoresystem wont function correctly 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