• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AlexFili · Jul 22, 2012 at 09:57 AM · editorplayerenemy

Player damage stops working over time

Here is the code for when the enemies attack the player in my simple chase-the-player game:

 var victim : Transform;
 var dieRange = 1.5;
 var explosion : GameObject;
 private var controller : CharacterController;
 
 function Start()
 {
     controller = GetComponent(CharacterController);
 }
 
 function Update()
 {
  var range = Vector3.Distance(victim.position, transform.position);
      
      if (range <= dieRange)
      {
       playerHP.healthPoints=playerHP.healthPoints-0.3;
       
       if(Random.value>0.85)
       {
       Instantiate (explosion, transform.position, transform.rotation);
       }
      }
 }

I'm having two major problems with this so far:

1) The main character takes damage, but after a short amount of time (about five seconds) the health will never go down anymore, so it will be stuck at a value like 47% or 56% and not go down. The enemies continue to chase and surround the player but nothing else happens.

2) I'm getting a recurring error on the command console window saying: "The variable victim of 'damagePlayer' has not been assigned." This is completely untrue because I have declared it both in the script and for each of the AI characters, so I have no idea what is going wrong here. (FIXED)

One of the other scripts I use is the one which modifies the text in the GUI to represent what health the player is at.

 static var healthPoints: int = 100;
 var targetGuiText: GUIText;
 
 function Start () {
 }
 
 function Update () {
  targetGuiText.text = "Health: "+(healthPoints)+"%";
 }

On a lesser note, when my First Person Character jumps, he flies off into the atmosphere and doesn't come back down. I'm using the default FPS character and have not touched the jumping mechanics whatsoever.

Probably worth noting that I'm using the A* Pathfinding Project to make the enemies head towards the player, I'm on a bumpy terrain and I've got eight enemies going at once.

It's working now, thanks for the help!

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Jul 22, 2012 at 10:01 AM 0
Share

Concerning your victim variable, have you dragged the object in the inspector? or place this in the start function:

 victim = GameObject.Find("NameOfTheObject").transform;
avatar image Seth-Bergman · Jul 22, 2012 at 10:17 AM 0
Share

I suspect it is something in the victim's script

by the way the line playerHP.healthPoints=playerHP.healthPoints-0.3; can be shortened:

playerHP.healthPoints-=.03;

just FYI

once the error begins, hit the pause button, and select your various enemies. you will find the victim var is empty. Then select your victim and see what's wrong with him... maybe you instance a new enemy? or disable/destroy the player?

as for the jumping, is there a var for gravity in the script; or jumpheight? it may just need the tweaking of vars, but I'm not familiar with the script offhand

avatar image fafase · Jul 22, 2012 at 10:20 AM 1
Share

$$anonymous$$mm, would there be in the PlayerHp script some kind of

 if(healthPoints<=0) Destroy(gameObject);

That could explain the health not going down anymore and the loss of the victim transform. I think that showing the other scripts involved would fix this real quick.

avatar image Bovine · Jul 22, 2012 at 11:29 AM 0
Share

You may want to cache the Transform in this script as well.

As @Fafase says, I suspect the Victim has 'died' in some way, or are you instantiating enemies who's victim is null and needs hooking up when they are instantiated?

avatar image Bovine · Jul 22, 2012 at 12:22 PM 1
Share

@$$anonymous$$Fili the same way you cache the controller, you can just add:

var my_transform : Transform;

To the local variables and then in Start() or Awake() you would do:

my_transform = transform.

I am a C# bod so I can't be sure if the above JS is correct, but it probably is.

I've never tried it but transform is probably very like GetComponent(Transform)

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Stay Back! 2 Answers

Follow within a certain distance? 1 Answer

A node in a childnode? 1 Answer

Player Die When Hit By Enemy 1 Answer

editor script makes editor crash 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges