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 Dwaring28720 · Oct 20, 2014 at 07:13 PM · c#guifloating-texthealth

how to display the damage value from a mob

hi im having a bit of a confusing time with this 1, im new to c# and unity but im learning as i go here, ive got a script for a mob to attack a player and it deducts from the players current health fine, now im trying to display the damage in a kind of floating combat text,

now ive got the text to display fine, im just unsure how to get the value that is being deducted and displaying that amount, ive commented the code as much as possible and im hoping someone can just point me in the right direction

 using UnityEngine;
 using System.Collections;
 
 public class mobatt : MonoBehaviour {
     public GameObject target;
     public float attacktimer;
     public int cooldown;
     float Damagedisp;
     private float scrollspeed; //scroll speed on the vertical 
 
     // Use this for initialization
     void Start () {
         attacktimer = ( Random.Range(1,3));
         cooldown = ( Random.Range(1,3));
         //---------
         scrollspeed = Screen.height /2-10; //<--text scrolling
 
         //---------
 
     }
     void  OnGUI ()
     {
         GUI.Label ( new Rect(Screen.width/2-25, scrollspeed, 50, 50), Damagedisp.ToString()); <--this is the position of the scrolling text
     }
     // Update is called once per frame
     void Update () {
         if (attacktimer > 0)
                         attacktimer -= Time.deltaTime;
         if (attacktimer < 0)
                         attacktimer = 0;
 
      {
                         if (attacktimer == 0) {
                                 attack ();
                 attacktimer = cooldown;
             
                         }
                 }
 
         //------
         scrollspeed -= Time.deltaTime*300; //<--scroll rate of the text
         //------
 
     }
 
     private void attack() {
         float distance = Vector3.Distance (target.transform.position, transform.position);
         Vector3 dir = (target.transform.position - transform.position), normalized;
         float direction = Vector3.Dot (dir, transform.forward);
 
         if (distance < 2.0f) {
                         if (direction > 0.13) {
                                 playerhp eh = (playerhp)target.GetComponent ("playerhp"); //this gets the value of the currenthp and the next line does the deductions
                 eh.adjcurhp (- Random.Range(1,5));
                 Damagedisp = 20; //<<<<<<---this is the part im having trouble with i cant seem to figure out what to put here, i was thinking Damagedisp = eh.adjcurhp but it doesnt seem to work
                         }
                 }
     }
 }
 

as i say i am very new to both unity and c# so i am willing to learn but obviously i need a little bit of guidance on the end bit,

thanks for any help on this 1

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 Dwaring28720 · Oct 20, 2014 at 07:27 PM 0
Share

alt text

this is the error i get when i try to change it to Damagedisp = eh.adjcurhp;

error.jpg (11.9 kB)

1 Reply

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

Answer by mwbranna · Oct 20, 2014 at 07:55 PM

Some stuff to optimize in here but I'll leave it expanded for clarity. Basically you just generate the number once and use that same value to do damage and to display instead of trying to pass the number in to "eh" and getting it back out.

 int damage = Random.Range(1,5);
 eh.adjcurhp(-1 * damage);
 Damagedisp = damage;
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 Dwaring28720 · Oct 20, 2014 at 08:23 PM 0
Share

SUPERB!!! short and sweet yeah i was trying to figure out a way of getting the value back from eh, i was considering looking at the health script then working the difference between current hp then the new current hp then i lost my head thinking about it, lol this again is perfect thanks very much, this kind of advice is what im always after because obviously its a perspective im not seeing at the moment

thanks again

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

Is there any easy way to keep buttons inside of box? (c#) 1 Answer

Erroe when resizing GUITextures in C# file 1 Answer

C# GUI Destroy or Disable? 1 Answer

Draw more EditorGUILayout components? 1 Answer

How can i show number of lives as image during gameplay 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