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

[SOLVED] Accessing string from another script

Discussion in 'Scripting' started by AtomicCabbage33, Nov 16, 2016.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I have a string in another script which gets updated every time the player switches weapons. Its purpose is to let my PlayerShoot script know which weapon we have equipped and then play the according sound. However, my code does not throw any errors although the sounds are not playing. I have tested the sound by placing the CmdPlayAwpShootSound(); and the CmdPlayTecShootSound();lines in the Start() function and the sounds played.

    String we are trying to access:

    Code (CSharp):
    1. public string currentHolder;
    2.  
    3. void Start ()
    4. {
    5.     currentHolder = "awpweaponHolder";
    6.     EquipAwpWeapon(primaryWeapon);
    7. }
    8.  
    9. void Update()
    10. {
    11.     if(Input.GetAxis("WeaponSwitch") >0f)
    12.     {
    13.         currentHolder = "tecweaponHolder";
    14.         EquipTecWeapon(secondaryWeapon);
    15.         Debug.Log("switched weapon");
    16.     }
    17.     else if (Input.GetAxis("WeaponSwitch")<0f)
    18.     {
    19.         currentHolder = "awpweaponHolder";
    20.         EquipAwpWeapon(primaryWeapon);
    21.         Debug.Log("switched weapon back");
    22.     }
    23. }
    PlayerShoot script:

    Code (CSharp):
    1. void Update ()
    2. {
    3.     currentWeapon = weaponManager.GetCurrentWeapon();
    4.  
    5.     if (PauseMenu.IsOn)
    6.         return;
    7.  
    8.     if (currentWeapon.fireRate <= 0f)
    9.     {
    10.         if (Input.GetButtonDown("Fire1") && weaponManager.currentHolder == "awpweaponHolder")
    11.         {
    12.             Shoot();
    13.             CmdPlayAwpShootSound();
    14.             Debug.Log("play awp sound");
    15.  
    16.         }else if (Input.GetButtonDown("Fire1") && weaponManager.currentHolder == "tecweaponHolder")
    17.         {
    18.             Shoot();
    19.             CmdPlayTecShootSound();
    20.             Debug.Log("play tec sound");
    21.         }
    22.  
    23.     } else
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Are your debug's printing out?
    Did you print what weaponManager.currentHolder is? I would put in a print statement right at the start of your if(currentWeapon.fireRate <= 0f) if statement to make sure you're getting the right value.

    Also, is weaponManager just a single script used to manage all your weapons?
     
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    Thanks for your reply, I realised that my code was not formatted properly and my if statements weren't actually getting executed :')