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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Call a function from other script error

Discussion in 'Scripting' started by ARares, Mar 16, 2017.

  1. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    When i play the game i got this error, how i can solve that?
    3297e65513ebce75f12bab3aa21b23d1.png

    Code:

    Code (CSharp):
    1.     SwichWeapons changeWeapon;
    2.  
    3.  
    4.     void Start()
    5.     {
    6.         changeWeapon = GameObject.FindWithTag(playerTag).GetComponent<SwichWeapons>() as SwichWeapons;
    7.     }
    8.  
    9.     void Update()
    10.     {
    11.         changeWeapon.GetComponent<SwichWeapons>().DefaultWeaponVoid();
    12.     }
    13.  
    14.  
    Full Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using CodeStage.AntiCheat.ObscuredTypes;
    5.  
    6. public class WeaponShop : MonoBehaviour
    7. {
    8.     [Header("-Tags-")]
    9.     public ObscuredString playerTag = "Player";
    10.  
    11.     [Header("-Weapon Settings 01-")]
    12.     public Button WeaponButton1;
    13.     public ObscuredInt WeaponPrice1 = 5000;
    14.     public ObscuredInt LevelForWeapon1 = 5;
    15.     public Text weaponPriceText1;
    16.     public GameObject blockedPannel;
    17.     public Text levelTextPermison;
    18.  
    19.     [Header("-Weapons Boolen Options-")]
    20.     [SerializeField]
    21.     private ObscuredBool weaponPsitol = true;
    22.     [SerializeField]
    23.     private ObscuredBool weaponAssaultRifle = false;
    24.  
    25.     SwichWeapons changeWeapon;
    26.  
    27.  
    28.     void Start()
    29.     {
    30.         changeWeapon = GameObject.FindWithTag(playerTag).GetComponent<SwichWeapons>() as SwichWeapons;
    31.  
    32.         WeaponButton1.interactable = false;
    33.  
    34.         weaponPsitol = true;
    35.         weaponAssaultRifle = false;
    36.  
    37.         weaponPriceText1.text = WeaponPrice1.ToString();
    38.     }
    39.  
    40.     void Update()
    41.     {
    42.         if(UpgradeMenu.Money >= WeaponPrice1)
    43.         {
    44.             WeaponButton1.interactable = true;
    45.         }
    46.         else
    47.         {
    48.             WeaponButton1.interactable = false;
    49.         }
    50.  
    51.         if(LevelSystem.PlayerLevel < LevelForWeapon1)
    52.         {
    53.             blockedPannel.SetActive(true);
    54.  
    55.             levelTextPermison.text = "You don't have level " + LevelForWeapon1;
    56.         }
    57.         else if(LevelSystem.PlayerLevel >= LevelForWeapon1)
    58.         {
    59.             blockedPannel.SetActive(false);
    60.         }
    61.  
    62.  
    63.  
    64.         if(weaponPsitol == true && weaponAssaultRifle == false)
    65.         {
    66.  
    67.             changeWeapon.GetComponent<SwichWeapons>().DefaultWeaponVoid();
    68.  
    69.             weaponPriceText1.text = WeaponPrice1.ToString();
    70.         }
    71.  
    72.         if (weaponAssaultRifle == true && weaponPsitol == false)
    73.         {
    74.  
    75.             changeWeapon.GetComponent<SwichWeapons>().weapon01Void();
    76.  
    77.             weaponPriceText1.text = "Owned";
    78.         }
    79.     }
    80.  
    81.     public void BuyWeapon01()
    82.     {
    83.         if (LevelSystem.PlayerLevel >= LevelForWeapon1)
    84.         {
    85.             if (UpgradeMenu.Money >= WeaponPrice1)
    86.             {
    87.                 weaponAssaultRifle = true;
    88.                 weaponPsitol = false;
    89.  
    90.                 UpgradeMenu.Money -= WeaponPrice1;
    91.             }
    92.         }
    93.     }
    94. }
    95.  
     
    Last edited: Mar 16, 2017
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Which line is line 67? (It should come up which you doubleclick the error)
     
  3. smacbride

    smacbride

    Joined:
    Jan 10, 2015
    Posts:
    50
    Your error message states that the error is on line #67, however your code above only has 14 lines in it. Is that code above from WeaponShop.cs?
     
  4. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    the t from Update:
    changeWeapon.GetComponent<SwichWeapons>().DefaultWeaponVoid();
     
  5. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I edit the post with the full code.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    changeWeapon is definitely null, which means that your FindWithTag() call is probably not finding it.

    Additionally, you don't need to use GetComponent<SwichWeapons> on it, because it's already a SwichWeapons object.
     
  7. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I added a [SerializeField] for changeWeapon, and is still null, And i don't have any idea why :))
     
    Last edited: Mar 16, 2017
  8. ARares

    ARares

    Joined:
    Mar 18, 2016
    Posts:
    167
    I Solved:

    Code (CSharp):
    1.         SwichWeapons changeWeapon = FindObjectOfType(typeof(SwichWeapons)) as SwichWeapons;
    2.         changeWeapon.DefaultWeaponVoid();