- Home /
Unity health problem and enemy colision
My 1st problem is that the enemies aren't colliding with any game objects or themselves (they have a box collider and a character controller) they however do collide with my main character (player)
My 2nd problem is that i am currently have a health system , it works fine when there only one enemy, but when there are two ore more enemies, it doesn't work properly. It seams to create two instances of its self
(example the enemy does 10 damage on attack, when 2 enemies are attacking at once it should take away 20, but it takes away 10), currently a health script is applied to the enemy prefab, the health script contains the GUI and code.
i can do it when a GUI text is on the scene so i can store the values (this requires the enemies to be on the scene before game starts), but when i try to instantiate the prefab the script cant find the GUI text. (i try to add it to the script but it doesnt let me it still says none 2D texture.
code........ (in the enemy script, when an attack is delt this method is called )both of these scripts are attached to the enemy Prefab var damage=10;
function health(){
var health : healthScript= GetComponent(healthScript);
health.hp(damage);
}
in the healthScript var healthtexture=gameObject.Find ("100%");
function hp(damage){
health-=damage; if (health<1){ healthtexture.guiTexture.texture= health00;
}....
Are your health and hp functions attached to scripts attached to the enemy? Are these functions intended to deal damage to the player or the enemy itself?
Answer by Emo · May 06, 2011 at 10:30 AM
i have solved the health problem by myself, probelm was it was creating instances of the highscore
created an GUI Texture added a png called 100% added the healthscript (take away damage includes different images for different health)
var damageManager = GameObject.FindWithTag("100%"); damageManager.GetComponent(healthScript).applyDamage(10);
Answer by lhk · May 01, 2011 at 02:37 PM
I don't understand exactly what you've done. My suggestion would be to change the code like this
enemy.js should look similar to this
var player : GameObject ;
function attack(damage : int) { player.broadcastMessage("applyDamage",damage); }
player.js should look similar to this
var hp : int =100;
function applyDamage(damage : int) { hp-=damage; //deal with your GUI here }
This way you get a neat cut between player and enemy. Moreover you can add new types of enemies very simple by just letting them sent applyDamage, too. However the most important thing is, that broadcastMessage tries to invoke applyDamage in every script on your player GameObject. Therefore when you try to implement animations you can add this method to your animation.js script and play an animation where your character backs away or stumbles.
Answer by lhk · May 01, 2011 at 02:52 PM
I made a few simple tests but on my machine Charactercontroller and Boxcollider worked together without problems. I don't know exactly what your problem is, so I can only guess:
- Are the enemies at the same heigth.
- Did you set one of the colliders to be a Trigger
- Did you make an ominous call to something like Physics.IgnoreCollision(). Maybe to deal with the attacks, to shoot a bullet ,...
I'm sorry I can't really help. If you provide more information I'll try to solve your problem, so far try:
- Make a really simple script to just move an enemy in one direction and provoke a "simple" collision. Turn off all the other scripts to make sure none of them is interfering. If the collision works, it is likely that one of your scripts is the problem
- Check everything twice. Maybe add another collider instead of the Boxcollider and remove the Boxcollider.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130635im_/https://answers.unity.com/themes/thub/images/avi.jpg)