Search Unity

Static variable in C sharp.

Discussion in 'Scripting' started by satre, Feb 16, 2013.

  1. satre

    satre

    Joined:
    Feb 10, 2013
    Posts:
    11
    Unity gives me that error: Assets/ProjectBase/C#/UI/deadGUI.cs(15,33): error CS0117: `PlayerMotor' does not contain a definition for `dead'. What I do wrong?

    Here is the code: deadGUI.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [System.Serializable]
    4.  
    5.  
    6. public class deadGUI : MonoBehaviour {
    7. void ShowDeadGui () {
    8.        
    9.     if (PlayerMotor.dead == true)    //trying to access to static variable in PlayerMotor.cs
    10.         {
    11.            
    12.         guiText.text = "You're dead";  
    13.            
    14.         }
    15.        
    16.     }
    17. }
    18.  
    And also PlayerMotor.cs code snipset that I trying to access. Variable dead was declared as public static bool dead = false;

    Code (csharp):
    1. void ApplyDamage(float dmg){
    2.   hitPoints -= dmg;
    3.   if (hitPoints>0){
    4.    
    5.   networkView.RPC("SetHP", RPCMode.AllBuffered, hitPoints);}
    6.   else{
    7.    Destroy();
    8.    bool dead = true;
    9.   }
    10.  
    11.  }
     
  2. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    Code (csharp):
    1.  
    2.     void ApplyDamage(float dmg){
    3.       hitPoints -= dmg;
    4.       if (hitPoints>0){
    5.        
    6.       networkView.RPC("SetHP", RPCMode.AllBuffered, hitPoints);}
    7.       else{
    8.        Destroy();
    9.        bool dead = true;
    10.       }
    11.      
    12.      }
    13.  
    try too just use dead = true; instead of bool dead = true;
    Because
    Code (csharp):
    1.  
    2. bool dead = true; //Means you are making a new bool variable called dead, with beginstate is true
    3. dead = true; //setting the boolean variable 'dead' to true
    4.  
     
  3. satre

    satre

    Joined:
    Feb 10, 2013
    Posts:
    11
    Hmm then unity pup up this message.

    Assets/ProjectBase/C#/OtherScripts/PlayerMotor.cs(315,5): error CS0103: The name `dead' does not exist in the current context
     
  4. satre

    satre

    Joined:
    Feb 10, 2013
    Posts:
    11
    Every thing is good, I write var at the top of the script, now I replaced it near my function and everything goes ok. Thank you.
     
unityunity