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 Dakk · Nov 17, 2011 at 01:28 PM · c#healthbar

How do i run a function from within another script?

Hello, i think i've solved the issue i ahd yesterday with accessing another script, i did remake one of them so that they both are made in c#. Now i have another issue.

What i want it to do is that when the box (enemyscripts "owner") is close enough to the player. I want it to run a damage function from within "healthbarscript". But i gett a null reference what not shit whenever he hits my player. I have no idea of why this is as it is.

Could somehere here spot the issue?

Healthbarscript:

 using UnityEngine;
 using System.Collections;
 
 
 public class HealthBarScript : MonoBehaviour {
     //varablar. Namnen talar för sig själva
     public int Maximumhealth = 100;
     public int CurrentHealth = 100;
     
     public float healthBarLength;
     
     // Use this for initialization
     void Start () {
         healthBarLength = Screen.width / 2;
     }
     
     // Update is called once per frame
     void Update () {
         AdjustCurrentHealth(0);
     }
     //Skapar healthboxen
     void OnGUI(){
         GUI.Box(new Rect(10, 10, healthBarLength, 20),
                 CurrentHealth + "/" + Maximumhealth);    
     }
     //Förhindrar division med 0 etc.
     public void AdjustCurrentHealth(int adj){
         CurrentHealth += adj;
         
         if(CurrentHealth < 0){
             CurrentHealth = 0;    
         }
         
         if(CurrentHealth > 100)
         {
             CurrentHealth = Maximumhealth;    
         }
         
         if(Maximumhealth < 1){
             Maximumhealth = 1;    
         }
         
         healthBarLength = (Screen.width / 2) * (CurrentHealth / (float)Maximumhealth);
     }
     //förlora hp functionen
     public void DamageFunction(int Damage)
     {
         CurrentHealth -= Damage;    
     }
 }
 
 
 
 Enemyscript:
 
 
 using UnityEngine;
 using System.Collections;
 
 public class EnemyMovement2 : MonoBehaviour {
     
     //Variabler
     public Transform target;
     new int moveSpeed = 3;
     new int rotationSpeed = 3;
     
     private Transform myTransform;
     
     void Awake(){
         //Gör så att man har sin egna transform redo
         myTransform = transform;    
     }
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
         //Sätter varablen target till ett GameObject
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         
         target = go.transform;
         //rotationen
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
         //rörelsen emot spelaren
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         //En debug linje för att se tydligare rörelser
         Debug.DrawLine(target.transform.position, myTransform.position);
         
         
         //Mäter avståndet emellan kuben och Playern
         float distance = Mathf.Sqrt( Mathf.Pow(target.position.x-myTransform.position.x,2) + Mathf.Pow(target.position.z-myTransform.position.z,2) );
         //Debug.Log(distance);
         
         //Mäter om den är tillräckligt nära för att "slå" dig
         if(distance < 1){
             Debug.Log("Fuck you, coptard");
             // Kör DamageFunction i "HealthBarScript"
             HealthBarScript healthbarscript = GetComponent<HealthBarScript>();
             healthbarscript.DamageFunction(5);
             
         }
     }
 
 }


Thanks in advance! :)

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
1

Answer by syclamoth · Nov 17, 2011 at 01:42 PM

The problems are entirely in your enemy script. There are a few non-critical issues here.

Do you really want to be using FindWithTag every single frame? Wouldn't it be better to do that just once in 'Start', (or at least only when target == null), since there should only ever be one player? FindWithTag is pretty slow, and you want to avoid using it when you don't have to.

As for the actual problem, it comes from this line-

 HealthBarScript healthbarscript = GetComponent<HealthBarScript>();

The problem there is that it is trying to find a healthbar on the same object as the EnemyScript! (which obviously doesn't work, because there isn't one.) You should instead use

 HealthBarScript healthbarscript = target.GetComponent<HealthBarScript>();

Which will return the correct component on the player, not the enemy.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

'The type or namespace name could not be found' 1 Answer

Distribute terrain in zones 3 Answers

Changing values of a script from another script not showing in game 1 Answer

Multiple Cars not working 1 Answer

SetActive() Object reference not set to an instance of an object. 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