Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by ImDose · Oct 04, 2016 at 05:40 PM · c#buttonfpsdoor

How can I have 2 Game Objects with the same script act independently?

In an FPS setting, I have 2 buttons, they both use the same script. I have a door object which I animate using purely code by using Lerp on Vector3's. I would like for the door to have a "button requirement", as in I'd have to press a certain number of buttons around the map before it opens. I do this by setting an int variable in the Door script. Which the button script then increments when pressed.

However, when pressing one button, instead of incrementing 1, it increments the same number of buttons that exist. i.e. if there's 3 buttons in the world, pressing 1 button is the same as pressing all 3 at the same time.

Also another question which is related is that the check is something like if (Input.GetButtonDown("Use") && !isPressed) , And then once the button is pressed I set isPressed to true. This is also setting it true for the other buttons in the world, how would I make it apply to just the button which was pressed?

Button Script:

 public class ButtonHandler : MonoBehaviour {
     public GameObject TextDisplay;
     public float Distance = PlayerCasting.DistanceToTarget;
     public GameObject Door;
 
     private bool isPressed = false;
 
     // Update is called once per frame
     void Update () {
         Distance = PlayerCasting.DistanceToTarget;
         if (Input.GetButtonDown("Use") && !isPressed)
         {
             if (Distance <= 5)
             {
                 isPressed = true;
                 OpenDoor.ButtonsPressed += 1;
             }
         }
     }

Door Script:

 public class OpenDoor : MonoBehaviour {
     public int ButtonRequirement;
     public int RaiseHeight = 8;
     public float speed = 0.2f;
 
     public static int ButtonsPressed = 0;
 
     private Vector3 _closedPosition;
     private Vector3 _endPos;
 
     // Use this for initialization
     void Start () {
         _closedPosition = transform.position;
         _endPos = _closedPosition + new Vector3(0, RaiseHeight, 0);
     }
     
     // Update is called once per frame
     void Update () {
         if (ButtonsPressed == ButtonRequirement)
         {
             StartCoroutine("OpenTheDoor");
         }
     }
 
     IEnumerator OpenTheDoor()
     {
         float t = 0f;
         Vector3 startPos = transform.position;
         while (t < 1f)
         {
             t += Time.deltaTime * speed;
             transform.position = Vector3.Lerp(startPos, _endPos, t);
             yield return null;
         }
     }
 }
Comment
Add comment · Show 2
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 TBruce · Oct 04, 2016 at 05:49 PM 1
Share

Could you post the complete script so we can better see what you are doing?

avatar image ImDose TBruce · Oct 04, 2016 at 05:53 PM 0
Share

@$$anonymous$$avina Just posted them.

0 Replies

· Add your reply
  • Sort: 

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

251 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I not set a trigger to happen? 1 Answer

OnClick() event script from a prefab 0 Answers

Referencing a button in script for different game modes in the Space Shooter Tutorial 0 Answers

Rapid Fire Script issues 1 Answer

LoadScene not Working 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