- Home /
Editing a variable from another script on collision
My goal is to be able to edit the variable(Current Health) from the first script when my projectile collides with an object containing AIHealth(script 1) from a second script. AI Skelly Prefab contains the AIHealth script. The second script cannot seem to find the variable in the other script.
First Script(AIHealth.cs):
using UnityEngine;
using System.Collections;
public class AIHealth : MonoBehaviour {
public float MaxHealth=100;
public float CurrentHealth;
public bool Dead;
void Awake () {
CurrentHealth=MaxHealth;
}
void Update() {
if(CurrentHealth<=0){
CurrentHealth=0;
Dead=true;
}
if(CurrentHealth>=MaxHealth)CurrentHealth=MaxHealth;
if(Dead){
}
}
}
Second Script(Damage.cs):
using UnityEngine;
using System.Collections;
public class Damage : MonoBehaviour {
public float damage=60;
public static int CurrentHealth = 100;
void OnCollisionEnter(Collision other){
if(other.gameObject.tag == "AI Skelly Prefab"){
var target = other.gameObject
var ch = target.transform.GetComponent("AIHealth").CurrentHealth;
ch -= damage;
}
}
}
If this is not possible, is there a different way to do it that accomplishes the same task?
Answer by sparkzbarca · Nov 12, 2012 at 06:12 AM
you declared a bool known as dead, you did not set it to false and bools by default are true I believe, so thats actually a bit odd as the object should in fact insta die. :P
the gameobject in this case is what? what exactly did you type for it?
Im wondering if you just ended up trying to destroy the script or something.
For example is you used the keyword this.
so Destroy(this); That would remove the script.
you need to make sure you pass it the actual AIEnemy
Lastly i dont think that ch variable thing is working, honestly the problem is with unity I'm not sure if unity is using pointers or not but I think in that case its not.
I think whats happening here is
lets say the aiscript has current health and its equal to 100;
so
aihealth = 100;
ch = get aihealth;
so
ch = 100;
now
ch = ch - 20;
so
ch = 80
BUT
aihealth still equals 100
thats because ch isn't aihealth. its a copy of aihealth.
what you need to do is get rid of what is honestly an unneeded variable anyways
just do
target.transform.GetComponent<AIHealth>().CurrentHealth -= damage;
well, bools will default to false, actually.. but otherwise dead on.
It should say:
void OnCollisionEnter(Collision other){
if(other.gameObject.tag == "AI Skelly Prefab")
other.gameObject.GetComponent<AIHealth>().CurrentHealth -= damage;
}
I changed the things you suggested and tested the game but no matter what I shot, it still does not do damage. I checked and it has nothing to do with the death.
I have done some tests, and it seems like the problem is in
void OnCollisionEnter(Collision other){ if(other.gameObject.tag == "Enemy"){ other.gameObject.GetComponent().CurrentHealth -= damage; Debug.Log("damage"); } }
}
The debug.log is not returning anything when I hit the enemy. Could it be a problem with the if statement?
I finally fixed it by messing with the gameObject tag. Thank you for all the help.
Answer by sparkzbarca · Nov 11, 2012 at 10:36 PM
you can't use the string version of getcomponent.
All you need to do is change this line to a type. dont forget the () after the type.
target.transform.GetComponent<AIHealth>().CurrentHealth;
see this
http://unitygems.com/mistakes1/
for that and other common mistakes
I changed that line of code and did some reading of the guide, which is very helpful, thank you for pointing me to it, and when I build the script it showed no errors. I also changed the if(Dead){ to include Destroy(gameObject); however, when I test the game, the enemy is not destroyed no matter the number of arrows.
you declared a bool known as dead, you did not set it to false and bools by default are true I believe, so thats actually a bit odd as the object should in fact insta die. :P
the gameobject in this case is what? what exactly did you type for it?
Im wondering if you just ended up trying to destroy the script or something.
For example is you used the keyword this.
so Destroy(this); That would remove the script.
you need to make sure you pass it the actual AIEnemy
Lastly i dont think that ch variable thing is working, honestly the problem is with unity i'm not sure.
I'm not sure if unity is using pointers or not but I think in that case its not.
I think whats happening here is
lets say the aiscript has current health and its equal to 100;
so
aihealth = 100; ch = get aihealth; so ch = 100;
but thats a copy
Answer by CostelloNicho · Nov 12, 2012 at 06:21 AM
Sounds like you should really be using a c# event. If I were you I would set up an entire event messaging system. In this way you can handle Broadcasted events within each script subscribed without outside scripts manipulating your variables (generally frowned upon). Here's a link to the c# messenger which I believe follows standard design paradigms: