- Home /
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;
}
}
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;
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'.
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;
wont let me drop the script or the game object that holds Boss$$anonymous$$anager script on it?
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.
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.
Your answer
Follow this Question
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