- Home /
checking Bool true from another Script?
I've looked around and cant work out why this script isn't working for me looks just like all the suggestions that people give others? so I want to check if the bool is true on another script and if it is then I can take damage. this is what I ended up with any help would be great. :D
Script 1 - BulletScript (placed on my bullet prefab)
public bool armed = false;
void OnCollisionEnter (Collision col)
{ if (col.transform.tag == "Shield")
{ armed = true;
print ("armed " );
}
Script 2 - Health System (placed on my enemy prefab)
void OnCollisionEnter (Collision col)
{ if (GameObject.Find("PlasmaOrb").GetComponent().armed == true && col.gameObject.tag == "Bullet")
{
GoobaHealth -= damage; }
}
Answer by jabez · Oct 12, 2017 at 02:34 AM
Either make a static script where you store variables, use getcomponent or add a reference to the script!
in script 2
add
public BulletScript bulletScript
;
to where you declare things in script2/health script.
everything in BulletScript needs to be declared as public so you can access them from another script. make sure to drag the bullet script onto script2.
now you can access everything by calling for it
bulletScript.armed = false;
if(bulletScript .armed){
}
tbh if i were you i'd use
https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
& use GetComponent
everything in BulletScript needs to be declared as public so you can access them from another script
https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
when you hit it
so something like
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.GetComponent<BulletScript> ()){
if (collision.gameObject.GetComponent<BulletScript> ().Armed == true) {
// do logic
}
}
}
or
void OnCollisionEnter(Collision collision)
{
BulletScript bulletScript = collision.gameObject.GetComponent<BulletScript> ;
if(bulletScript){
if (bulletScript.Armed == true) {
// do logic
}
}
}
Here is some stuff to read/watch about statics too, might be handy in the future. https://unity3d.com/learn/tutorials/topics/scripting/statics
You're most welcome, If you need a hand feel free to message me. I hope your game comes along nicely, Good luck C:
Answer by gnp89 · Oct 12, 2017 at 02:18 AM
This is not clear at all
sorry fixed up should look clearer now not sure why it bunched it all toget in one paragraph