C#C
C#4y ago
43 replies
cccc

Update Variable Between Scripts

Hey there, this isnt my first time updating between scripts so I dont entirely know the issue.
I have two scripts, 'GunController' (with the variable: 'public bool canFire;') in which I am trying to update in the script 'BulletController'
in theory i have the canfire updated in the start() of bulletcontroller for when the prefab spawns to make the bool false
and then when the object gets destroyed, it is then set to true
i will simplify the code to the main parts to make it obvious
BULLETCONTROLLER:
 GunController gunScript;
    // Start is called before the first frame update
    void Start()
    {
        reelBack = GameObject.FindGameObjectWithTag("POINT");
        m_Rigidbody = GetComponent<Rigidbody>();
        gunScript.canFire = false;
    }

    // Update is called once per frame
    void Update()
    {
        freezeTime -= Time.deltaTime;   //new
        if (freezeTime <= 0)
        {
            m_Rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ;
            bulletFreeze -= Time.deltaTime;
            if (bulletFreeze <= 0)
            {
                Destroy(gameObject, .2f);
                transform.LookAt(reelBack.transform);
                transform.position += transform.forward * 100*Time.deltaTime;
                gunScript.canFire = true;
            }
        }
        if (freezeTime > 0)
        {
            transform.Translate(Vector3.up * shootSpeed * Time.deltaTime);
            
        }
Was this page helpful?