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 missMien · Apr 22, 2015 at 05:28 PM · referencedamagehealth

Health won't work.

Hi. I'm making a game which requires me to have a healthscript and a seperate damage script. I tried following a tutorial from unity. But it won't work. I am still new at this, although I know a little bit. So can anyone please help me? Just try to tell me what's wrong because I've no clue. I would be so grateful.

Here's my Health Script

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class HealthScript : MonoBehaviour {
 
     public int startingHealth = 3;                           
     public int currentHealth;                                   
     public Slider healthSlider;                                 
     
     
     void Awake ()
     {
 
     
         currentHealth = startingHealth;
     }
     
     
     void Update ()
     {
 
     
     }
     
     
     public void TakeDamage (int amount)
     {
         
         // Reduce the current health by the damage amount. cuz that has to happen
         currentHealth -= amount;
         
         // Set the health bar's value to the current health. cuz that's cool (amirite?)
         healthSlider.value = currentHealth;
         
 
         if(currentHealth <= 0)
         {
             // ... it should die. so please die
             Destroy (gameObject);
             Debug.Log ("Yesh this works.");
         }
     }
     
     
 
 }
 

And this is my Move/Damage script. Yes it's a bit messy. sorry

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class moveP1 : MonoBehaviour {
     //Move and wall stuff
     public KeyCode upKey;
     public KeyCode downKey;
     public KeyCode rightKey;
     public KeyCode leftKey;
 
     public float speed;
 
     public GameObject wallPrefab;
     public GameObject BulletPrefab;
 
     Collider2D wall;
     Collider2D Bullet;
 
     Vector2 lastWallEnd;
 
     public float orthographicSize;
 
     //health stuff
     HealthScript playerHealth; 
     GameObject player;
 
     public int attackDamage;
     public GameObject other;
 
     bool isHit = false;
     bool bulletHit = false;
 
 
     void Start () 
     {
         player = GameObject.FindGameObjectWithTag ("player");
         playerHealth = player.GetComponent <HealthScript> ();
 
         Camera.main.orthographicSize = 80;
         GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
         spawnWall();
         InvokeRepeating ("BulletSpawner", 1f, 3f-(Time.deltaTime));
 
         // health stuff
 
 
     }
     
 
     void Update () 
     {
 
 //move
         if (Input.GetKeyDown (upKey)) {
             GetComponent<Rigidbody2D> ().velocity = Vector2.up * speed;
             spawnWall ();
         } else if (Input.GetKeyDown (downKey)) {
             GetComponent<Rigidbody2D> ().velocity = -Vector2.up * speed;
             spawnWall ();
         } else if (Input.GetKeyDown (rightKey)) {
             GetComponent<Rigidbody2D> ().velocity = Vector2.right * speed;
             spawnWall ();
         } else if (Input.GetKeyDown (leftKey)) {
             GetComponent<Rigidbody2D> ().velocity = -Vector2.right * speed;
             spawnWall ();
         }
     
         fitColliderBetween (wall, lastWallEnd, transform.position);
         //Health?
         if (isHit) 
         {
             playerHealth.TakeDamage (3);
             isHit = false;
         }
 
         if(bulletHit)
         {
             playerHealth.TakeDamage (1);
             Destroy (other );
             bulletHit = false;
         }
         //Loading levels
         if (GameObject.Find("player 1") ) 
         {
             //Debug.Log ("Don't die!");
         } 
         else 
         {
             Application.LoadLevel("player 2 won");
         }
         
         
         if (GameObject.Find("player 2") ) 
         {
             //Debug.Log ("Don't die!");
         } 
         else 
         {
             Application.LoadLevel("player 1 won");
         }
     }
 
     void BulletSpawner() 
     {
         GameObject rr = (GameObject)Instantiate (BulletPrefab, new Vector3(5, 0, 0), Quaternion.Euler(0,0,Random.Range(0,360)));
         Bullet = rr.GetComponent<Collider2D> ();
     }
 
     void spawnWall()
     {
         lastWallEnd = transform.position;
 
         GameObject g = (GameObject)Instantiate(wallPrefab, transform.position, Quaternion.identity);
         wall = g.GetComponent<Collider2D>();
     }
 
     void fitColliderBetween(Collider2D co, Vector2 a, Vector2 b) 
     {
         co.transform.position = a + (b - a) * 0.5f;
           
         float dist = Vector2.Distance(a, b);
         if (a.x != b.x)
             co.transform.localScale = new Vector2(dist + 1, 1);
         else
             co.transform.localScale = new Vector2(1, dist + 1);
     }
     
 
     void OnTriggerEnter2D(Collider2D co) 
     {
         
         if (co != wall) 
         {
             isHit = true;
         }
         
         if (co = Bullet) 
         {
             bulletHit = true;
 
         }  
 
 
     }
 
 
 
 }


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 SamTheT · Apr 22, 2015 at 06:01 PM

you can make an instance of the healthscript in your damage script:

 HealthScript myHealth = GameObject.FindOfObjectOfType<HealthScript>();

that probably will solve it

Comment
Add comment · Show 1 · 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 missMien · Apr 22, 2015 at 06:12 PM 0
Share

Do I have to write something in the () after FindObjectOfType?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Object reference not set to an instance of an object 1 Answer

Health Bar Only For Falling Damage 2 Answers

blood damage like call of duty? 2 Answers

Health Bar Help 2 Answers

How to create/fix fire damage script???? 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