- Home /
How to make two scripts share same variable?
OK, my FPS is coming along slowly but Ive hit a problem. I have sliding doors in my game that work fine using triggers BUT now I want to add a key so that certain doors will only open if the player has this key.
Im using one very basic script called 'playerInventory' which has a Boolean variable called 'hasKey' (I have this attached to my player)
My other script is attached to the key itself -
var pickUp : AudioClip;
var hasKey : boolean;
function OnTriggerEnter ()
{
AudioSource.PlayClipAtPoint(pickUp, transform.position);
hasKey = true;
Destroy(gameObject);
}
For the main part all works well, key is destroyed on contact with trigger, sound plays and object gets destroyed.
My problem is that as soon as the object is destroyed the 'hasKey' variable doesn't exist. How do I get the variable from this script to be recognised by my playerInventory script?
Answer by Benproductions1 · Jul 13, 2013 at 10:20 AM
Hello,
Your problem, is that you are keeping your hasKey
variable, inside the "key". Instead you should keep it on the character.
Because you have not provided any further information, it's impossible to tell how the rest of your systems work and I can't give you a definitive answer on what you should do.
However (if you haven't already) you should look up "accessing variables from another script".
Hope it helps,
Benproductions1