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 How to you see if a bool is true for false on a different object

Discussion in 'Scripting' started by ThatGuyWithAMask, Apr 2, 2023.

  1. ThatGuyWithAMask

    ThatGuyWithAMask

    Joined:
    Jun 21, 2022
    Posts:
    9
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Switch : MonoBehaviour
    6. {
    7.    public PlayerInput playerInput;
    8.  
    9.     public GameObject Weapon;
    10.     public GameObject AK;
    11.     public GameObject Rifle;
    12.  
    13.     public GunBox pistolBox;
    14.     GunBox gunBox;
    15.  
    16.     public void Start()
    17.     {
    18.       pistolBox.GetComponent<GunBox>();
    19.     }
    20.  
    21.     public void OnPistol()
    22.     {
    23.       PistolChange();
    24.     }
    25.  
    26.     public void OnAk()
    27.     {
    28.       AkChange();
    29.     }
    30.     public void OnRifle()
    31.     {
    32.       RifleChange();
    33.     }
    34.  
    35.     public void PistolChange()
    36.     {
    37.    
    38.       Weapon.gameObject.SetActive(true);
    39.       AK.gameObject.SetActive(false);
    40.       Rifle.gameObject.SetActive(false);
    41.    
    42.     }
    43.  
    44.     public void AkChange()
    45.     {
    46.       Weapon.gameObject.SetActive(false);
    47.       AK.gameObject.SetActive(true);
    48.       Rifle.gameObject.SetActive(false);
    49.     }
    50.     public void RifleChange()
    51.     {
    52.       Weapon.gameObject.SetActive(false);
    53.       AK.gameObject.SetActive(false);
    54.       Rifle.gameObject.SetActive(true);
    55.     }
    56.  
    57.  
    58. }
    59.  
    60.  
    i am trying to get refence from the HasPistol bool.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class Weapon : MonoBehaviour
    7. {
    8.  
    9.     public bool HasPistol;
    10.     private PlayerInput controls;
    11.  
    12.     public GameObject bulletPrefab;
    13.     public GameObject shellSpawn;
    14.     public float EjectSpeed = 400f;
    15.     public float ShelLife = 2f;
    16.  
    17.     public Transform player;
    18.  
    19.     public Camera cam;
    20.     private RaycastHit rayHit;
    21.     public GameObject Gun;
    22.     public Animation anim;
    23.  
    24.     public TextMeshProUGUI Ammo;
    25.  
    26.     [SerializeField] private float bulletRange;
    27.     [SerializeField] private float fireRate, reloadTime;
    28.     [SerializeField] private bool isAutomatic;
    29.     [SerializeField] private int magazineSize;
    30.     public int ammoLeft;
    31.  
    32.     private bool isShooting, readyToShoot, reloading;
    33.  
    34.     [SerializeField] public GameObject BulletHolePrefab;
    35.     [SerializeField] private float BulletHoleLifeSpan;
    36.     [SerializeField] private ParticleSystem muzzleFlash;
    37.     [SerializeField] private ParticleSystem Smoke;
    38.     [SerializeField] string EnemyTag;
    39.     [SerializeField] string bombTag;
    40.     [SerializeField] private float horizontalSpread, verticalSpread, burstDelay;
    41.     [SerializeField] private int bulletsPerBurst;
    42.     private int bulletsShots;
    43.  
    44.  
    45.  
    46.     private void Awake()
    47.     {
    48.         ammoLeft = magazineSize;
    49.         readyToShoot = true;
    50.         controls = new PlayerInput();
    51.  
    52.         cam = Camera.main;
    53.  
    54.         controls.Player.Shoot.started += ctx => StartShot();
    55.         controls.Player.Shoot.canceled += ctx => EndShot();
    56.  
    57.         controls.Player.Reload.performed += ctx => Reload();
    58.     }
    59.     private void Start()
    60.     {
    61.         HasPistol = true;
    62.     }
    63.  
    64.     private void Update()
    65.     {
    66.  
    67.         Ammo.text = ammoLeft.ToString();
    68.  
    69.         if (isShooting && readyToShoot && !reloading && ammoLeft > 0)
    70.         {
    71.             bulletsShots = bulletsPerBurst;
    72.             PerfromShot();
    73.         }
    74.     }
    75.  
    76.     private void StartShot()
    77.     {
    78.         isShooting = true;
    79.     }
    80.     private void EndShot()
    81.     {
    82.         isShooting = false;
    83.  
    84.     }
    85.     private void PerfromShot()
    86.     {
    87.         readyToShoot = false;
    88.         Gun.GetComponent<Animator>().Play("Shoot");
    89.  
    90.         float x = Random.Range(-horizontalSpread, horizontalSpread);
    91.         float y = Random.Range(-verticalSpread, verticalSpread);
    92.  
    93.         Vector3 direction = cam.transform.forward + new Vector3(x, y, 0);
    94.  
    95.         if(Physics.Raycast(cam.transform.position, direction, out rayHit, bulletRange))
    96.         {
    97.             Debug.Log(rayHit.collider.gameObject.name);
    98.             if(rayHit.collider.gameObject.tag == EnemyTag)
    99.             {
    100.                 rayHit.collider.GetComponent<AIController>().TakeDamage(20);
    101.              
    102.             }
    103.  
    104.             if(rayHit.collider.gameObject.tag == bombTag)
    105.             {
    106.                 rayHit.collider.GetComponent<bom>().TakeDamage(20);
    107.              
    108.             }
    109.             else
    110.             {
    111.                 GameObject bulletHole = Instantiate(BulletHolePrefab, rayHit.point + rayHit.normal * 0.001f, Quaternion.identity) as GameObject;
    112.                 bulletHole.transform.LookAt(rayHit.point + rayHit.normal);
    113.                 Destroy(bulletHole, BulletHoleLifeSpan);
    114.             }
    115.  
    116.         }
    117.  
    118.         muzzleFlash.Play();
    119.         Smoke.Play();
    120.         ammoLeft --;
    121.         bulletsShots --;
    122.  
    123.         GameObject Shel = Instantiate(bulletPrefab, shellSpawn.transform.position, shellSpawn.transform.rotation) as GameObject;
    124.         Rigidbody ShelRig = Shel.GetComponent<Rigidbody>();
    125.         ShelRig.AddForce(ShelRig.transform.right * EjectSpeed);
    126.         Destroy(Shel, ShelLife);
    127.  
    128.         if(bulletsShots > 0 && ammoLeft > 0)
    129.         {
    130.             Invoke("ResumeBurst", burstDelay);
    131.         }
    132.         else
    133.         {
    134.             Invoke("ResetShot" , fireRate);
    135.             if(!isAutomatic)
    136.             {
    137.                 EndShot();
    138.             }
    139.         }
    140.  
    141.      
    142.  
    143.  
    144.     }
    145.  
    146.     private void ResumeBurst()
    147.     {
    148.         readyToShoot = true;
    149.         PerfromShot();
    150.      
    151.      
    152.     }
    153.     private void ResetShot()
    154.     {
    155.         readyToShoot = true;
    156.     }
    157.     private void Reload()
    158.     {
    159.         Gun.GetComponent<Animator>().SetBool("NeedsR", true);
    160.         reloading = true;
    161.         Invoke("ReloadFinish" , reloadTime);
    162.         Gun.GetComponent<Animator>().Play("Reload");
    163.     }
    164.     private void ReloadFinish()
    165.     {
    166.         ammoLeft = magazineSize;
    167.         reloading = false;
    168.         Gun.GetComponent<Animator>().SetBool("NeedsR", false);
    169.     }
    170.     private void OnEnable()
    171.     {
    172.         controls.Enable();
    173.     }
    174.     private void OnDisable()
    175.     {
    176.         controls.Disable();
    177.     }
    178.  
    179.  
    180.  
    181.  
    182. }
    183.  
    i have it so when you pick up the weapon the bool is true but i don't know how to check if the bool is true or false in the weapon change script.
     
  2. ThatGuyWithAMask

    ThatGuyWithAMask

    Joined:
    Jun 21, 2022
    Posts:
    9
    Never mind i did some fiddling around with it and it works now.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
    Bunny83 likes this.