C
C#2y ago
c_ccc

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);

}
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);

}
27 Replies
Kouhai
Kouhai2y ago
What issue(s) are you having?
c_ccc
c_ccc2y ago
so currently nothing is happening the bullet spawns, canFire does not update within the gun (despite being in the start function) the bullet despawns, canFire doesnt update
c_ccc
c_ccc2y ago
c_ccc
c_ccc2y ago
hoggy077
hoggy0772y ago
your gunscript isn't assigned by the looks
Kouhai
Kouhai2y ago
Okay, in your update loop you set canFire to true if both freezetime and bulletFreeze are less than 0, right?
c_ccc
c_ccc2y ago
and just so u are aware of what the entire script does:
c_ccc
c_ccc2y ago
yeah thats what im attempting to do in my mind currently, if its under start(), canfire should be false and when freeztime and bulletfreeze are both 0 and the prefab begins to destroy, canfire should be true 'GunController gunScript;' is that not fully assigning?
hoggy077
hoggy0772y ago
Unfortunately no Since Gun controller (im assuming) is on your gun for your character, its an instance per say. So to access it you would need to assign it cause its essentially like any normal class, you can have a variable for it, but unless you assign it your code wont know what or where to change anything
c_ccc
c_ccc2y ago
ah u see originally when ive done it in the past i would assign it as if it were a game object but because the prefab doesnt currently exist if i public the gunScript
c_ccc
c_ccc2y ago
c_ccc
c_ccc2y ago
i cant drag the controller in is there a way to assign it properly through code would get component work>?
hoggy077
hoggy0772y ago
get component would work, but you would need a reference to the game object with your GunController script on it
Kouhai
Kouhai2y ago
if gunScript wasn't set, it would've thrown a NullReferenceException
c_ccc
c_ccc2y ago
it did i double checked on both lines
hoggy077
hoggy0772y ago
additionally, if you dont want a reference to your gameobject (or to get it using some other method like FindGameObjectWithTag) you could use FindObjectOfType and use typeof(GunController)
c_ccc
c_ccc2y ago
sorry, didnt clarify
Kouhai
Kouhai2y ago
Ok, if it's does thrown NullReference, then follow what hoggy077 is saying 👍 Ik it's pedantic 😅 But I would personally recommend using the generic version FindObjectOfType<T> instead of typeof
hoggy077
hoggy0772y ago
actually do that one that way you dont need to cast the result
c_ccc
c_ccc2y ago
hmm as in like this
c_ccc
c_ccc2y ago
hoggy077
hoggy0772y ago
add () on the end FindObjectOfType is a function
c_ccc
c_ccc2y ago
AH U LEGENDS thank you so much
hoggy077
hoggy0772y ago
been a while, but you might also be able to call it without GameObject. but i may be wrong
c_ccc
c_ccc2y ago
that was way more difficult because of the prefab normally i just drag and drop whatever it is through public i didnt know how to do it like that thank you guys
hoggy077
hoggy0772y ago
all good
c_ccc
c_ccc2y ago
<3 have a good day