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 /
avatar image
0
Question by paetznick01 · May 11, 2015 at 12:13 PM · c#errorhealth

Heath regain via Pickup code halp me

So I am trying to develop a pickup script but nothing I do seems to be working. I get a boatload of errors and everything I do to fix it seems to have that close but no cigar thing going for it. the code is in c# and it uses two scripts to execute one to manage player heath and the other exists to destroy the game object depending on whether or not the player is at full health. Note that 90% of this code is reverse engineered from various parts of the survival shooter unity project.

I beleive the second code is bug free

first code named PlayerHealth

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour
 {
     public int startingHealth = 100;
     public int currentHealth;
     public Slider healthSlider;
     public Image damageImage;
     public AudioClip deathClip;
     public float flashSpeed = 5f;
     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
 
 
 
    
     AudioSource playerAudio;
     PlayerShooting playerShooting;
     bool isDead;
     bool damaged;
     bool AtFullHealth;
     bool Regain;
     CapsuleCollider CapsuleCollider;
 
 
     void Awake ()
     {
        
         playerAudio = GetComponent <AudioSource> ();
         playerShooting = GetComponentInChildren <PlayerShooting> ();
         currentHealth = startingHealth;
         CapsuleCollider = GetComponent <CapsuleCollider> ();
     
     }
 
 
     void Update ()
     {
         if(damaged)
         {
             damageImage.color = flashColour;
         }
         else
         {
             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
         }
         damaged = false;
     }
 
 
 
     void OnCollisionEnter(Collider other)
     {
             if(other.gameObject.tag == "FirstAid")
         {
                 CapsuleCollider.isTrigger = true;
                 Regain = true;
                 currentHealth += 25;
                 healthSlider.value = currentHealth;
 
         }
     
         if(currentHealth >= 100 && !AtFullHealth)
         {
             IsAtFullHealth ();
         }
     }
 
     void IsAtFullHealth ()
     {
         AtFullHealth = true;
 
         Regain = false;
     }
 
     public void TakeDamage (int amount)
     {
         damaged = true;
 
         currentHealth -= amount;
 
         healthSlider.value = currentHealth;
 
         playerAudio.Play ();
 
         if(currentHealth <= 0 && !isDead)
         {
             Death ();
         }
     }
 
 
     void Death ()
     {
         isDead = true;
 
         playerShooting.DisableEffects ();
             
 
         playerAudio.clip = deathClip;
         playerAudio.Play ();
 
             playerShooting.enabled = false;
 
         Application.LoadLevel ("LoseScene");
     }
 
 }
 


















second code begins here

 using UnityEngine;
 using System.Collections;
 
 public class HealthUp : MonoBehaviour 
 {
 
     GameObject player;
     bool PickedUp; 
     PlayerHealth playerHealth;
     BoxCollider BoxCollider;
  
     void Awake ()
     
     {
         player = GameObject.FindGameObjectWithTag ("Player");
         BoxCollider = GetComponent <BoxCollider> ();
         playerHealth = player.GetComponent <PlayerHealth> (); 
      }
     
     void OnTriggerEnter (Collider other)
     {
         if(other.gameObject == player)
 
             if (playerHealth.currentHealth <= 100)
 
                 {
 
                     
                     
                     BoxCollider.isTrigger = true;
                     Destroy (gameObject);
                 }
 
     }
 }    
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · May 13, 2015 at 09:09 PM

Honestly, I would remove the functionality from the second class, and in the collision code for the first one, when it collides with a powerup, add:

 Destroy(other.gameObject);

If you're getting errors though, pay close attention, because they will always* lead you to the problem. (or at least google or post them if you don't know what they mean, so we can explain).

Another thing is to always understand what every single line of code does. Don't import from another project unless you know what that code is doing and how. I've been programming for a decade and I still follow that rule because any time I don't, I end up with headaches.

*well, at least 99.9% of the time.

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

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Need help calling a variable from another C# script 1 Answer

Distribute terrain in zones 3 Answers

Hey guys, im starting do make a RPG game and in the Level system script(I called it "PlayerStatsController") i have an error: Parser Error : Unexpected symbol `public'. My script above: 1 Answer

Health Script/Targeting system c# 2 Answers


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