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 Koharu · May 17, 2014 at 07:34 PM · variablehealth bar

Can't access variable from another script

When a player catches boost(additional life) I want to access my health script and add +1 to the currentHealth. It doesn't work. Here my boost life script:

     public GameObject particles;
     public bool isOnTrigger = false;
 
     void OnTriggerEnter (Collider other) {
         if (other.tag == "Player") {
             GameObject.FindWithTag("Player").GetComponent<PlayerHealth>().currentHealth+=1;
             GameObject part = Instantiate(particles,transform.position,Quaternion.identity)as GameObject;
             Destroy(part,2);
             Destroy (this.gameObject);
         }
 
     }
 }

This is HealthScript attached to the player:

 public int currentHealth;
     private int startHealth = 7;
     private GUIStyle styleForHealth;
     private GUIStyle healthBarBG;
     private float healthBarLength;
     private bool showHealthBar = true;
 
 
     void Start(){
         currentHealth = startHealth;
     }
 
     void Update () {
         var scoreScr = Camera.main.GetComponent<ScoreScript> ();
         currentHealth = startHealth - scoreScr.miss - scoreScr.crush;
 
         if (currentHealth > startHealth)
             currentHealth = startHealth;
         if (currentHealth < 0)
             currentHealth = 0;
         if (currentHealth<1) {
             GameObject.FindWithTag("Player").GetComponent<PlayerController>().canControl = false;
             Invoke ("LevelStart", 2);
         }
         healthBarLength = ((Screen.width / 4 ) /startHealth)*currentHealth;
         if (healthBarLength < 1) {
             showHealthBar = false;
             healthBarLength = 0;
         }
     }
 
     void LevelStart(){
         Application.LoadLevel (2);
     }
     void OnGUI(){
         InitStyles();
         GUI.Box (new Rect(Screen.width/2+195,10,Screen.width/4+10,30),"  ",healthBarBG);
         if(showHealthBar)
             GUI.Box (new Rect(Screen.width/2+200,15,healthBarLength,20),currentHealth+"/"+startHealth, styleForHealth);
 
     }
     private void InitStyles(){
         if( styleForHealth == null ){
             styleForHealth = new GUIStyle( GUI.skin.box );
             styleForHealth.normal.background = MakeTex( 2, 2, new Color( 0.4f, 0.8f, 0f, 1f ) );
         }
         if (healthBarBG == null) {
             healthBarBG = new GUIStyle(GUI.skin.box);
             healthBarBG.normal.background = MakeTex(2,2, new Color(0f, 0.4f, 0f, 1f) );
         }
     }
     private Texture2D MakeTex( int width, int height, Color col ){
         Color[] pix = new Color[width * height];
         for( int i = 0; i < pix.Length; ++i ){
             pix[ i ] = col;
         }
         Texture2D result = new Texture2D( width, height );
         result.SetPixels( pix );
         result.Apply();
         return result;
     }

Thank you in advance.

Comment
Add comment · Show 6
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 Burla · May 17, 2014 at 09:07 PM 0
Share

Can you specify the phrase "It isn't working."

Do you have any error messages in the console that you can post? Is something else happening other than "It isn't working."? :-)

avatar image Koharu · May 17, 2014 at 09:18 PM 0
Share

No, I don't have any error messages, but the variable currentHealth is not changing.

avatar image Herman-Tulleken · May 17, 2014 at 09:31 PM 0
Share

And I assume all the other stuff happens (the destroys etc.), right?

avatar image Koharu · May 17, 2014 at 09:55 PM 0
Share

Yes, everything else happens. I think that the problem might be in the way I calculate currentHealth: currentHealth = startHealth - scoreScr.miss - scoreScr.crush; But, still, it shouldn't affect that I am changing it from another script.

avatar image MrFijiWiji · May 17, 2014 at 10:14 PM 0
Share

Well to begin with i'd remove both GetComponent calls from the Update function, create 2 variables and cache them from the Start function. As it is you're getting the scoreSCR every frame which i'm sure you don't want, and the same goes for the player which will fire for every frame that health is below 1.

what exactly is miss and crush?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by getyour411 · May 17, 2014 at 11:18 PM

Any change to currentHealth (as in +1) would be lost since PlayerHealth Update() recalcs it every frame. You need to redesign that bit

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

23 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 avatar image avatar image

Related Questions

Change variable from another script - C# 1 Answer

accessing vars of other script doesn't work 1 Answer

Need to use 2 different language scripts. 1 Answer

Basic C# Public Variable Help 3 Answers

Static Variable Problem 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