Search Unity

Accessing Counter from a different Script?

Discussion in 'Scripting' started by ecloev, Oct 12, 2015.

  1. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Hello,
    I am making a counter and am trying to access a value from a different script and set it equal to a int in a seperate script. Here are the two scripts? Why am I getting an error trying to get iCounter?
    Thanks!

    First script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerHealth : MonoBehaviour {
    7.    
    8.     public float Health = 600;
    9.     public int healthRate = 5;
    10.     public float timer = 0F;
    11.     public bool playerDead = false;
    12.     public double counter = 0;
    13.     public int iCounter;
    14.     public Text t;
    15.  
    16.    
    17.     public void damagePlayer (int damage){
    18.         Health -= damage;
    19.         if (Health <= 0)
    20.         {
    21.             Dead();
    22.         }
    23.     }
    24.    
    25.     private void Dead(){
    26.         Destroy (this.gameObject);
    27.         playerDead = true;
    28.         Application.LoadLevel("RespawnMenu");
    29.     }
    30.     private void Update ()
    31.     {
    32.         timer += Time.deltaTime;
    33.         counter += Time.deltaTime;
    34.         iCounter = (int)counter;
    35.         t.text = "" + iCounter;
    36.         if (timer > 2) {
    37.             Health += healthRate;
    38.             timer = 0;
    39.         }
    40.         if (Health > 600) {
    41.             Health = 600;
    42.         }
    43.     }
    44.        
    45.      }
    46.  
    47.  
    Second script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class RespawnCounter : MonoBehaviour {
    7.     public int FinalCount;
    8.     public Text t;
    9.  
    10.  
    11.     void Update(){
    12.         FinalCounter ();
    13.     }
    14.     public void FinalCounter(){
    15.         FinalCount = iCounter;
    16.         t.text = "" + FinalCount;
    17.  
    18.    
    19.     }
    20. }
    21.  
    22.  
     
  2. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102