- Home /
Global script?
Hi all I was hoping someone would be able to help me. I have a script in c# for OnMouseDown to pick up a key, picking up said key creates a sound and destroys it. I want to make it so that the door I need the key for is locked and cannot be opened at all, but once I pick up the key the door unlocks and I can interact with the door (which has it's own separate script). Could someone show me how I would do this please? I believe it has something to do with a global script but that is why I am here :) Thank you for any help in advance.
If needed the script I have for my key is:
public class KeyPickup : MonoBehaviour { public GUIText KeyHover; public AudioClip keygrab;
void OnMouseOver()
{
KeyHover.text = "The key for the door at Reception";
}
void OnMouseExit()
{
KeyHover.text = null;
}
void OnMouseDown()
{
StartCoroutine("KeyPickUp");
audio.enabled = true;
light.enabled = false;
}
IEnumerator KeyPickUp()
{
Debug.Log ("Picked up key");
renderer.enabled = false;
collider.enabled = false;
yield return new WaitForSeconds (3f);
}
}
Answer by reineinmyheart · Dec 08, 2014 at 07:18 PM
There are a lot of solutions..
If I understand what you needed correctly,
One would be to use the GetComponent method.
Have a boolean inside the door's script that checks whether it's locked or not (by default locked is set to true), and make it public.
Now, your KeyPickup script would be setting the door's locked variable to true.
So your KeyPickup script must look something like this:
public NameOfYourDoorScript door_script;
void Awake()
{
door_script = GameObject.Find("NameOfDoor").GetComponent<NameOfYourDoorScript>();
}
void OnMouseDown()
{
door_script.locked = false;
}
Check the use of GetComponent in the docs, Hope this helps.
Thank you ever so much for your help, it has saved me a great deal of stress as this is for my winter submission and it was one of the last few things that I needed to do for my level.
@SSJ-Panda glad to be of help! please mark it as an answer if this helped you solve your question. goodluck!