Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by revanl · May 22, 2014 at 11:59 AM · playerregeneration

Player regeneration

Hello i am new to programming I want my Player to regenerate Health when he steps inside a box collider i'm using these scripts

 //playerHealth.js
     var healthTexture : Texture2D;
     var healthBorder : Texture2D;
     var adjust : int;
     static var health : int = 100; // static lets it be accessed by other scripts (eg: playerHealth.health = xxxxx)
       
 function Update(){
     adjust = health * 3;
     }
        
 function OnGUI () {
     
 
     GUI.DrawTexture(Rect(43,Screen.height - 65,314,36), healthBorder);
     GUI.BeginGroup(Rect(55,Screen.height - 55,adjust,15));
     GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
     GUI.EndGroup();
     }

The next script is my player takes damage when he steps inside a collider

 //damage.js
 
 var enter : boolean = false;
 
 var damage = 10;
 
  
 
 function Update () {
 
 if(enter == true){
 
 playerHealth.health -= damage * Time.deltaTime;
 
 }
 
 }
 
  
 
  
 
 function OnTriggerEnter (other : Collider){
 
  
 
 if (other.gameObject.tag == "Player") {
 
 enter = true;
 
 }
 
 }
 
  
 
  
 
 function OnTriggerExit (other : Collider){
 
  
 
 if (other.gameObject.tag == "Player") {
 
 (enter) = false;
 
 }
 
 }

And this is my regeneration when inside a collider but nothing happens. It's almost the same as the damage script exept that i changed the "playerHealth.health -= damage Time.deltaTime;" to "playerHealth.health += damage Time.deltaTime;" //regeneration.js

 var enter : boolean = false;
 
 var regen = 10;
 
  
 
 function Update () {
 
 if(enter == true){
 
 playerHealth.health += regen * Time.deltaTime;
 
 }
 
 }
 
  
 
  
 
 function OnTriggerEnter (other : Collider){
 
  
 
 if (other.gameObject.tag == "Player") {
 
 enter = true;
 
 }
 
 }
 
  
 
  
 
 function OnTriggerExit (other : Collider){
 
  
 
 if (other.gameObject.tag == "Player") {
 
 (enter) = false;
 
 }
 
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Zodiarc · May 22, 2014 at 12:16 PM

If the playerHealth.js is attached to the player model, try the following in the regeneration.js:

     var playerHealth:playerHealth;
     
     function Start(){
       playerHealth = GameObject.FindWithTag("Player").GetComponent("playerHealth");
     }
 
 // Your Update function....
 
 
 
Comment
Add comment · Share
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
0

Answer by revanl · May 23, 2014 at 11:21 AM

I tried what you said but it gives me an error "UnityEngine GameObject.GetComponent" for the argument list "playerHealth" was not found

 var enter : boolean = false;
 var regen = 10;
 var playerHealth:playerHealth;
  
 function Start(){
 playerHealth = GameObject.FindWithTag("Player").GetComponent(playerHealth);
 }
  
 function Update () {
  
 if(enter == true){
  
 playerHealth.health += regen * Time.deltaTime;
  
 }
  
 }
  
  
  
  
  
 function OnTriggerEnter (other : Collider){
  
  
  
 if (other.gameObject.tag == "Player") {
  
 enter = true;
  
 }
  
 }
  
  
  
  
  
 function OnTriggerExit (other : Collider){
  
  
  
 if (other.gameObject.tag == "Player") {
  
 (enter) = false;
  
 }
  
 }
Comment
Add comment · Show 2 · Share
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 Zodiarc · May 23, 2014 at 09:27 PM 0
Share

That was my fail.

playerHealth = GameObject.FindWithTag("Player").GetComponent("playerHealth");

should do the job. The script is simply missing a reference to playerHealth. By adding this line, you tell the script where it can find the variable.

I also updated my answer.

avatar image revanl · May 24, 2014 at 07:33 PM 0
Share

It worked thanks :)

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.

Follow this Question

Answers Answers and Comments

21 People are following this question.

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

Related Questions

Regain health on GUI 2 Answers

How do i make my player die? 2 Answers

Player to Gui Object Communication in C# 1 Answer

How to Destroy Copies made during levelload? 2 Answers

Object Disappearing when comes close to player. 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