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 lijrobert · Jan 14, 2012 at 09:42 PM · c#variblehealth

Problems With Health pack Script

I'm placing this code

 using UnityEngine;
 using System.Collections;
 public class HealthTrigger : MonoBehaviour {
     // Use this for initialization
     void Start () {
     }
     // Update is called once per frame
     void Update () {
     }
     void OnTriggerEnter(Collider other)
     {
         GameObject Player = GameObject.Find("Player");
         PlayerHealth playerhealth = Player.GetComponent<PlayerHealth>();
         playerhealth.curHealth += 10;
         Destroy (gameObject);
         audio.Play();
     }
 }

on a heart container in my adventure game, it gets the value of curHealth and is supposed to add ten health disapeer and play a sound, any Help? P.S. Yes Heart container is a trigger

Comment
Add comment · Show 1
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 Bunny83 · Jan 15, 2012 at 03:16 AM 0
Share

There is a "101 010"-button above your edit-window. It will format the selected text as code. All it does is this:

  • Indent every line by 4.

  • add a black line before and after the code-block.

This will make the text being displayed as code.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Robinmtb · Jan 15, 2012 at 12:35 AM

Hi!

First I'll repost your code with breaks so it's easier to read :D

 using UnityEngine;
 using System.Collections; 
 public class HealthTrigger : MonoBehaviour { 
   //Use this for initilization 
   void Start () { } // Update is called once per frame
   void Update () { }
 
   void OnTriggerEnter(Collider other) { 
     GameObject Player = GameObject.Find("Player");
     PlayerHealth playerhealth = Player.GetComponent(); 
     playerhealth.curHealth += 10; 
     Destroy (gameObject); 
     audio.Play();
   }
 }


Ok, first off, the reason your audio isn't playing is because you destroy the gameObject before you try to play it. A good solution to this is:

 playerhealth.curHealth += 10;
 renderer.enabled = false; //Objects mesh isn't visible 
 audio.Play();
 yield WaitForSeconds (audio.clip.length); //Waits until audio is finished
 Destroy (gameObject);

Now for the problem with the health not being added.

I think the reason for this could be that you are working with a local instance of the component. Try writing a method in PlayerHealth like:

 void AddHealth(int health){
   curHealth += health;
 }

and see if that works.

Are you sure you're getting the correct component? I'm not used to C# so I haven't seen GetComponent used like that without anything in the () before :)

Good Luck! I hoped I was to some help!

Robin

Comment
Add comment · Show 4 · 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 lijrobert · Jan 15, 2012 at 02:28 AM 0
Share

Thats perfect thank you, one question how did you get the code to format like that

avatar image lijrobert · Jan 15, 2012 at 03:17 AM 0
Share

Darn it I knew it was probably something simpe like that, Thanks Robinmtb

avatar image Bunny83 · Jan 15, 2012 at 03:22 AM 0
Share

@lijrobert: Oh and use "add new comment" if you want to comment ;). Answers should always answer the question. If you figured out the answer yourself, feel free to post an answer yourself.

I've converted you answer into a comment. I see you already posted another comment, your "answer" was stuck in the moderation queue to prevent spam. As soon as you have 16+ karma you can post directly.

avatar image lijrobert · Jan 15, 2012 at 03:46 PM 0
Share

@Bunny83 Thank you for the help, I'll remember, I just joined so have to figure some of the stuff out :D

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# add helth over time 1 Answer

hi I know that this script is seen often but I have an another problem with a damage and health scripts. I think it's the GetComponent part, well anyway if you could help that would be great. 2 Answers

c# help please 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