Hello guyz, I'm working on a survival game and got many help from youtube but i get problems,I have a scipt named "EnemyLogic" Before i added animation and the weapon ,The Enemy damage stopped working Script code Code (csharp): #pragma strict var Health : int = 100; function Update () { if (Health <= 0) { Dead(); } } function ApplyDamage (TheDamage : int) { Health -= TheDamage; } function Dead() { Destroy (gameObject); }
Ummm.... You might need to provide more information. What exactly stopped? The death? Taking damage? Compiler errors?
No, Change ApplyDamage to: Code (CSharp): function ApplyDamage (TheDamage : int) { Debug.Log("Hello from ApplyDamage"); Health -= TheDamage; } Then run your game, attack stuff and check the unity console. There should be some hello messages there.
No messages in the console? That means that method is never being called. Please show me the code that calls the ApplyDamage function.
Code (csharp): #pragma strict var Health : int = 100; function Update () { if (Health <= 0) { Dead(); } } function ApplyDamage (TheDamage : int) { Health -= TheDamage; } function Dead() { Destroy (gameObject); }
ApplyDAMAGE Code Code (csharp): #pragma strict var Damage : int = 50; var Distance : float; var MaxDistance : float = 1.5; var TheMace : Transform; function Update () { if (Input.GetButtonDown("Fire1")) { TheMace.animation.Play("attack"); var hit : RaycastHit; if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit)) { Distance = hit.distance; if (Distance < MaxDistance) { hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver); } } } }
Yeah I HIT AN ENEMY UnityEngine.Debug:Log(Object) EnemyLogic:Main() (at Assets/EnemyLogic.js:18) now?
Change var MaxDistance : float = 1.5; to var MaxDistance : float = 10; Your problem is that MaxDistance is too low
I think the SendMessage is not working, try using GetComponent instead Code (CSharp): Distance = hit.distance; if (Distance < MaxDistance) { hit.transform.GetComponent(script name).ApplyDamage(Damage); } } } } Or try using gameObject.SendMessage instead of transform.SendMessage Code (CSharp): Distance = hit.distance; if (Distance < MaxDistance) { hit.gameObject.SendMessage ("ApplyDamage", Damage); } } } }
(20,32): BCE0023: No appropriate version of 'UnityEngine.Component.GetComponent' for the argument list '(UnityEngine.Transform)' was found.
Code (JavaScript): #pragma strict var Damage : int = 50; var Distance : float; var MaxDistance : float = 1.5; var TheMace : Transform; function Update () { if (Input.GetButtonDown("Fire1")) { TheMace.animation.Play("attack"); var hit : RaycastHit; if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit)) { Distance = hit.distance; // HERE Debug.Log(hit.distance); if (Distance < MaxDistance) { hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver); } } } }
UnassignedReferenceException: The variable TheMace of 'MeleeSystem' has not been assigned. You probably need to assign the TheMace variable of the MeleeSystem script in the inspector. MeleeSystem.Update () (at Assets/MeleeSystem.js:11)
So assign the mace object in the inspector? I am getting the feeling you dont know what you are doing? And are just using someones scripts. You need to stop and learn the basics first.
Have you been through the training sessions we have in the learn site? http://unity3d.com/learn/ Do roll a ball and space shooter to get the basics down first.