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 DrunkenHorror · Aug 03, 2014 at 04:06 AM · damage

Taking Damage

     var FireballDMG : int = 8;
     var bulletSpeed     : float = 15;
 
 function Update ()
 
 {
     transform.Translate(0, 0, bulletSpeed * Time.deltaTime);
     }     
     
     function OnCollisionEnter(collision : Collision){
         if(collision.gameObject.CompareTag("Boss")){
         var curHealth -= FireballDMG;
         }
     }

okay so i have that on my fireball prefab but unity is telling me unexpected token "-="? and here is my object script taking the damage.

 public var curHealth : int = 100;
 var maxHealth : int = 100;
 var healthtext : GUIText;
 public var curMana : int = 100;
 var maxMana : int = 100;
 var manatext : GUIText;
 
 function Update () {
 
     manatext.text = curMana + " / " + maxMana;
 
     if(curMana < 0 ) {
         curMana = 0;
     }
 
     if(curMana > 100) {
         curMana = 100;
     }
     
     healthtext.text = curHealth + " / " + maxHealth;
 
     if(curHealth < 0 ) {
         curHealth = 0;
     }
 
     if(curHealth > 100) {
         curHealth = 100;
     }
 }




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

Answer by Kiwasi · Aug 03, 2014 at 04:12 AM

  • makes no sense in the context. And the compiler is telling you so.

  • will subtract the right hand value from the left hand value and set the left hand value to the result. In this case you are declaring a variable, and using -= on the same line.

You can resolve this by using:

 scriptToTakeDamage.curHealth -= FireballDMG;
Comment
Add comment · Show 7 · 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 DrunkenHorror · Aug 03, 2014 at 04:19 AM 0
Share

okay so i tried that ins$$anonymous$$d and i am now getting /FireBall forward.js(12,13): BCE0020: An instance of type 'Boss$$anonymous$$anager' is required to access non static member 'curHealth'.

avatar image Kiwasi · Aug 03, 2014 at 05:06 AM 0
Share

Do you have an instance of Boss$$anonymous$$anager?

You need something like

 // Assign via the editor
 public var boss$$anonymous$$anager : Boss$$anonymous$$anager;
 
 // Inside a function
 boss$$anonymous$$anager.curHealth -= FireballD$$anonymous$$G;

avatar image DrunkenHorror · Aug 03, 2014 at 05:19 AM 0
Share

wont let me drop the script or the game object that holds Boss$$anonymous$$anager script on it?

avatar image Kiwasi · Aug 03, 2014 at 05:22 AM 0
Share

Is Boss$$anonymous$$anager a $$anonymous$$onoBehavior?

avatar image DrunkenHorror · Aug 03, 2014 at 05:26 AM 0
Share

no just a JS script the one i posted above tho if i make a game object from my fireball prefab i can drop Boss$$anonymous$$anager gameobject into the field and it does take damage off? just dont know why i cant have that field set for the prefab.

Show more comments
avatar image
0

Answer by Alpha_Guac · Oct 30, 2015 at 09:12 PM

here is what I use to affect another classes variable, I hope this helps.

public class FireBall : MonoBehaviour {

 public int fireballDMG = 8;
 public float speed = 15;
 
 void Update () 
 {
     transform.Translate(0, 0, speed * Time.deltaTime);
 }
 
 void OnCollisionEnter(Collision otherObject)
 {   //compares the name of the object to the string
     if(otherObject.gameObject.name=="Boss")
     {
         //calls the function TakeDamage from the otherObject,passing the int and won't break the game if it isn't recieved
         //function is case sensitive
         SendMessage("TakeDamage", fireballDMG, SendMessageOptions.DontRequireReceiver);
     }
 }

}

public class BossScript : MonoBehaviour { int Health = 100, maxHealth = 100, Mana = 100, maxMana = 100; GUIText healthText, manaText;

 void Update()
 {
     manaText.text = Mana + "/" + maxMana;
     healthText.text = Health + "/" + maxHealth;

     if (Mana <= 0)
         Mana = 0;
     if (Health <= 0)
         Health = 0;
     if (Health >= maxHealth)
         Health = maxHealth;
     if (Mana >= maxMana)
         Mana = maxMana;
 }
 // the function that is called from the sendMessage function uses local int Health
 void TakeDamage(int damageTaken)
 {
     Health -= damageTaken;
 }

}

Sending Messages can be useful because you don't need a global variable.

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

24 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Damage rate HELP 1 Answer

Bullet not destroying on collision 1 Answer

Damage taking? 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 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