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. Dismiss Notice

error CS0120

Discussion in 'Scripting' started by ARares, Apr 21, 2016.

  1. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167

    Weapon.Damage line:

    UpgradeMenu Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class UpgradeMenu : MonoBehaviour {
    6.  
    7.     public GameObject upgradeMenu;
    8.  
    9.     public static bool upgradeMenuEnabled = false;
    10.  
    11.     public GameObject pressToUpgradeMenu;
    12.  
    13.     public GameObject moneyText;
    14.  
    15.     public Button buttonHealth;
    16.     public Button buttonSpeed;
    17.     public Button buttonDmg;
    18.  
    19.     [SerializeField]
    20.     private int startingMoney;
    21.     public static int Money;
    22.  
    23.     public int dmgToBuy;
    24.  
    25.  
    26.     void Start ()
    27.     {
    28.         upgradeMenu.SetActive(false);
    29.  
    30.         pressToUpgradeMenu.SetActive(true);
    31.  
    32.         moneyText.SetActive(true);
    33.  
    34.         Money = startingMoney;
    35.  
    36.         buttonHealth.interactable = false;
    37.         buttonSpeed.interactable = false;
    38.         buttonDmg.interactable = false;
    39.     }
    40.  
    41.     void Update ()
    42.     {
    43.  
    44.         if (Money >= 100)
    45.         {
    46.             buttonHealth.interactable = true;
    47.         }
    48.  
    49.         if (Money >= 50)
    50.         {
    51.             buttonSpeed.interactable = true;
    52.         }
    53.  
    54.         if (Money >= 100)
    55.         {
    56.             buttonDmg.interactable = true;
    57.         }
    58.  
    59.         if (Input.GetKeyDown(KeyCode.U))
    60.         {
    61.             pressToUpgradeMenu.SetActive(false);
    62.  
    63.             if (upgradeMenuEnabled == true)
    64.             {
    65.                 upgradeMenuEnabled = false;
    66.                 upgradeMenu.SetActive(false);
    67.                 moneyText.SetActive(true);
    68.                 Time.timeScale = 1f;
    69.             }
    70.             else {
    71.                 upgradeMenuEnabled = true;
    72.                 upgradeMenu.SetActive(true);
    73.                 moneyText.SetActive(false);
    74.                 Time.timeScale = 0f;
    75.             }
    76.         }
    77.  
    78.         if (Input.GetKeyDown(KeyCode.Escape))
    79.         {
    80.             if (upgradeMenuEnabled == true)
    81.             {
    82.                 upgradeMenuEnabled = false;
    83.                 upgradeMenu.SetActive(false);
    84.                 moneyText.SetActive(true);
    85.                 Time.timeScale = 0f;
    86.             }
    87.         }
    88.     }
    89.  
    90.     public void closeUpgradeMenu()
    91.     {
    92.         upgradeMenuEnabled = false;
    93.         upgradeMenu.SetActive(false);
    94.         moneyText.SetActive(true);
    95.         Time.timeScale = 1f;
    96.     }
    97.  
    98.     public void UpgradeHealth()
    99.     {
    100.         if (Money >= 100)
    101.         {
    102.  
    103.         }
    104.     }
    105.  
    106.     public void UpgradeDmg()
    107.     {
    108.         if (Money >= 100)
    109.         {
    110.             Weapon.Damage += dmgToBuy;
    111.         }
    112.     }
    113. }
    114.  
     
  2. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Use this.Damage instead.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the script doesn't contain a "Damage" variable...

    If you are trying to upgrade the currently equipped weapon you need to have some sort of reference, or a means to gain that reference (i.e. reference to current player which can be used to gain a reference to it's current weapon).
     
  4. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Oh, sorry about that :p.

    You need a reference to an object, or if all your weapons do the same damage, you can declare Damage as static.
     
  5. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    is just one weapon
     
  6. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Just put "static" in the declaration then. Though, I'd advice you to learn whether a variable is a class member or an instance member, more generally about variable scope. It's part of the fundamentals of Object Oriented Programming (OOP).