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:
27 Replies
What issue(s) are you having?
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
your gunscript isn't assigned by the looks
Okay, in your update loop you set canFire to true if both freezetime and bulletFreeze are less than 0, right?
and just so u are aware of what the entire script does:
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?
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
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
i cant drag the controller in
is there a way to assign it properly through code
would get component work>?
get component would work, but you would need a reference to the game object with your GunController script on it
if gunScript wasn't set, it would've thrown a NullReferenceException
it did
i double checked
on both lines
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)
sorry, didnt clarify
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 typeofactually do that one
that way you dont need to cast the result
hmm as in like this
add () on the end
FindObjectOfType is a function
AH
U LEGENDS
thank you so much
been a while, but you might also be able to call it without
GameObject.
but i may be wrongthat 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
all good
<3 have a good day