- Home /
Other
Accessing non-static bools across scripts
I need to figure out which way my player is facing which I have in the PlayerAnimator Script using this
if(rigidbody.velocity.x > 0.25f && direction == false){
direction = true;
}
if(rigidbody.velocity.x < -0.25f && direction == true){
direction = false;
}
I need to put the direction boolean into another script called Death1. I could just copy paste the above code since it's not very long, but I figure I might as well learn the proper way to do it.
The PlayerAnimator is actually destroyed prior to Death1 being instantiated.
When the Players health hits 0 hes destroyed and replaced with a prefab for that death. For example if hes killed by a mine hes destroyed and MineDeath is instantiated. Can I still get the Direction even after PlayerAnimator is no longer in the scene?
Closed: Im just gonna use playerprefs
Answer by Sessional · May 01, 2015 at 05:10 PM
You are going to want to be using a GetComponenet() call. As long as the thing you are accessing is public you'll be able to reach it.
I'm assuming these two scripts will be on the same object?
They're not on the same object. Can I still be using get component? The gameobject holding PlayerAnimator is destroyed before death1 exists.
If that is the case you can not access that variable variable any more, because that script (PlayerAnimtor) would have been destroyed before death1 exists.
You could try disabling PlayerAnimator before hand and getting the variable and then destroying PlayerAnimator after that.
Answer by hbalint1 · May 01, 2015 at 05:11 PM
just make the direction to public. don't need to be static.
public bool direction;
then you can search for the object and script, which contains this.
GameObject.Find("TheObjectHavingTheScript").GetComponent<TheScriptContainsDirection>().direction;
The gameobject that has PlayerAnimator attached is destroyed Prior to the death1 script being in the scene. I just added more info to the question.
after you destroy the playeranimator of course you can't get any information from it. But if you get the last direction value and store it in the Death1 script befor you destroy the object, you can use it. But you will lose control over the direction change.