Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Cannot access static variable of another script.

Discussion in 'Scripting' started by psykick1, May 23, 2020.

  1. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    Hello,

    Im trying to make currncy.

    there is a script that check if "we have enough money"

    of courst CurretTurretToBuild , and cost is static variables.
    actually what happends , that the program ignoring
    " if (MoneyText.Money >= Turret.cost)"




    Code (CSharp):
    1.  void OnMouseDown ()
    2.     {
    3.         if (MoneyText.Money >= Turret.cost)
    4.         {
    5.             BuildManager.CurrentTurretToBuild = TurretToBuild;
    6.         }
    7.         else
    8.         {
    9.             Debug.Log("Not Enough Money");
    10.         }
    11.            
    12.        
    13.     }
    This is the script of the node
    Code (CSharp):
    1.  void OnMouseDown()
    2.     {
    3.  
    4.         if (currentTurretFound != null)
    5.         {
    6.             Debug.Log("Cannot Build There");
    7.         }
    8.         else
    9.         {
    10.            
    11.        
    12.             currentTurretFound = BuildManager.CurrentTurretToBuild;
    13.             MoneyText.Money -= Turret.cost;
    14.             BuildTurret = (GameObject)Instantiate(currentTurretFound, transform.position, transform.rotation);
    15.          
    16.        
    17.         }
    18.     }
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    i would imagine you're confusing what static does, but it's hard to tell without all the code.

    If you make Turret.cost a static variable, that means that every single turret will cost that much, not each instance or each type of turret but every single one. There can only be one value for each class. You're probably trying to set different values for each turret and it's only keeping the last value and hence you think the Turret.cost is broken.
     
  3. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    Hi,

    I have only 1 turret .. this is why im still confused.
     
  4. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    Can we get the full classes of the MoneyText and Turret classes? I don't think there's enough here to really tell what's going wrong.
     
  5. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    yep maybe your money is not public, would need to see your moneytext script