- Home /
Problem with health and level reload system
So I am making a game, and I cannot figure how to make a basic collision system. I want the player lose health when they collide with a certain GameObject, and reload the level after a certain number of hits. The health and damage should be editable, along with the GameObject. This is the way I did it-
var Enemy : GameObject;
var health = 10;
var damage = 2;
function Update(){ //what to do with a function inside a function?
function OnCollisionEnter(collision:Collision) {
if(collision.gameObject.name = Enemy){ //refering to the var
health -= damage;
}
if(health < 1){
Application.LoadLevel(Application.LoadedLevel); //I think that I have the right syntax for this....
}
}
}
//how would I add a hitscreen via GUI text?
//some very interesting complier errors
//thanks in advance, I'm new, and need all the help I can get
I need the answer urgently, can someone look over the code and find my mistakes? -Thanks in advance
A trimmed down version of the code, that compiles properly, but does not work.
var health = 5;
var damage = 1;
function OnCollisionEnter(collision:Collision) {
if(collision.gameObject.tag == "Enemy"){
health -= damage;
}
if(health < 1){
Application.LoadLevel(Application.LoadedLevel);
}
}
How doesn't it work? Throw some Debug.Log calls in there and see what is/isn't being called etc.
I just spoke to one of my friends that knows quite about Unity. He said that since the game is first person, the FP controller is not considered a proper collider. I assume that means I have to add a capsual collider to replace it...
I've never bothered with unity's controllers but from what I've seen of them, that sounds correct.
Was my answer correct? If so can you mark it by clicking on the gray tick icon (under the thumbs down icon). Otherwise you can ask for more help or even post the answer yourself. This helps keep the site clean.
I also see that this hasn't been done for all 9 of your questions. Can you please take a moment to give credit, where due, to the volunteers who give their time to help? Thanks.
Answer by Khada · Mar 14, 2013 at 12:33 AM
Issues I can see:
(1) Variable name Enemy should be changed to enemy. All variables should begin with a lower case letter, while all methods/types should begin with an upper case letter to prevent confusion or conflicts in the code.
(2) The OnCollisionEnter method should not exist within another function. I'd be very surprised if that script even compiles. Put all new functions inside the class but outside other functions. Edit: Or just outside other methods if you are in javascript.
(3) You're comparing an object with an object name on line 7. Instead you want to check an object name against and object name. You're also using the assignment operator (a single '='), not the comparison operator (double '='). Thus line 7 should read:
if(collision.gameObject.name == enemy.name)
Answer by DryTear · Mar 15, 2013 at 02:23 AM
instead of "OnCollider" use "OnControllerColliderHit", also do "Debug.Log("ItWorks");" to check wheres the error. Enemy should me non-capital, infact heres a script - attach it to the enemy:
function OnTriggerEnter(x : Collider)
{
if(x.gameObject.tag == "Player")
{
x.gameObject.SendMessage("GotDamaged");
}
}
^^^ Attach to enemy, create an empty game object and attach a collide with isTrigger to true to the player, then this script onto the player:
var health : float;
var damage : float;
function GotDamaged()
{
health -= damage;
}
correct me if im wrong
Your answer
Follow this Question
Related Questions
How do I delete an object on collision with another object? 1 Answer
I have a problem with implementing a health and death system, help? 1 Answer
How to do basic health & damage on collision 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer