Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SSJ-Panda · Dec 07, 2014 at 10:59 PM · c#globaldoorspick-up

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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SSJ-Panda · Dec 08, 2014 at 10:40 PM 0
Share

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.

avatar image reineinmyheart · Dec 09, 2014 at 03:44 PM 0
Share

@SSJ-Panda glad to be of help! please mark it as an answer if this helped you solve your question. goodluck!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Camera enable/disable problem. 1 Answer

Help. Newbie trying to have more than one button powered door a scene 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges