Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by watswat5 · Oct 08, 2013 at 12:11 AM · c#buttonarraydoorslide

Call a variable from each game object in an array?

I have two game objects. The first one, a sliding door, is controlled by a script called SlideDoorListener.cs

 using UnityEngine;
 using System.Collections;
 
 public class SlideDoorListener : MonoBehaviour {
     public GameObject button;
     private bool openPlayed;
     private bool closePlayed;
 
     // Use this for initialization
     void Start () {
         openPlayed = false;
         closePlayed = false;
     }
     
     // Update is called once per frame
     void Update () {
         if(button.GetComponent<ButtonTrigger>().isDown == true && openPlayed == false){
             animation.Play("Sliding Door");
             openPlayed = true;
             closePlayed = false;
         }
         if(button.GetComponent<ButtonTrigger>().isDown == false && closePlayed == false){
             animation.Play ("Close Door");
             closePlayed = true;
             openPlayed = false;
             }
     }
 }


I then have a button with a trigger and ButtonTrigger.cs attached:

 using UnityEngine;
 using System.Collections;
 
 public class ButtonTrigger : MonoBehaviour {
     public bool isDown;
     
     // Use this for initialization
     void Start () {
         isDown = false;
     }
     
     // Update is called once per frame
     void OnTriggerEnter (Collider other) {
         if(other.gameObject.tag == "Cube" && isDown == false)
             isDown = true;
             
     }
     void OnTriggerExit (Collider other){
         if(other.gameObject.tag == "Cube" && isDown == true)
             isDown = false;
     }
     }

SlideDoorListener checks to see if isDown in ButtonTrigger is true, which happens when an object tagged "Cube" enters the button's trigger area. If isDown is true, the slide door opens. It all works quite well.

However, if I want the door to be controller by more than one button, all of which must have isDown == true, then it's a pain. I'd have to make a new listener that incorporates the specific number of button's I'd like to use.

Can I use a GameObject array to add as many or few buttons as I'd like, call the isDown variable from all of them at once, and check to see if any of the isDown's are false?

I'm a beginning scripter, so I'm not too familiar with arrays and what they can do.

Comment
Add comment · Show 4
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 TrickyHandz · Oct 08, 2013 at 12:43 AM 0
Share

Tell me if I'm interpreting you correctly on what you want to do you here. You have one door that opens when a trigger is activated. You want multiple triggers to be able to open the same door. Is that correct?

avatar image watswat5 · Oct 08, 2013 at 12:44 AM 0
Share

I want multiple triggers to be REQUIRED to open the door.

avatar image watswat5 · Oct 08, 2013 at 12:45 AM 0
Share

I$$anonymous$$ I have to push down two buttons to open one door.

avatar image TrickyHandz · Oct 08, 2013 at 12:45 AM 0
Share

Ah, thank you for the clarification. I'll have something for you in a few $$anonymous$$utes.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TrickyHandz · Oct 08, 2013 at 01:24 AM

Okay, so here is a revision of the SlidingDoorListener that can track multiple triggers:

 using UnityEngine;
 using System.Collections;
 
 public class SlidingDoorListener : MonoBehaviour 
 {
     // An array to track all of
     // our buttons.  Since we need
     // access to a single component
     // the array is made of that type
     // rather than having to dig through
     // a gameObject.  This way only
     // triggers with the ButtonTrigger
     // script attached can be added in
     // the inspector
     public ButtonTrigger[] buttons;
 
     // Use a single bool to
     // track the state of the
     // door rather than one for
     // each state
     private bool IsOpen = false;
     
     void Update () 
     {
         int buttonsDown = 0;
 
         // Iterate through our list
         // of buttons and see how many
         // have been triggered
         for (int i = 0; i < buttons.Length; i++)
         {
             if (buttons[i].IsDown)
                 buttonsDown++;
         }
 
         // if all our buttons are triggered
         // and the animation hasn't been played
         // we toggle door state and play
         // the animation
         if (buttonsDown == buttons.Length && !IsOpen)
         {
             animation.Play("Sliding Door");
             IsOpen = false;
         }
 
         // Otherwise we play our close
         // door animation and toggle
         // set is Open to false
         else
         {
             animation.Play("Close Door");
             IsOpen = false;
         }
     }
 }


Here is the ButtonTrigger script:

 public class ButtonTrigger : MonoBehaviour 
 {
     // IsDown Initializes as false
     public bool IsDown = false;
 
     void OnTriggerEnter(Collider other)
     {
         // Toggle IsDown if the entering
         // gameObject has the "Cube" tag
         if (other.gameObject.tag == "Cube")
             IsDown = !IsDown;
     }
 
     void OnTriggerExit(Collider other)
     {
         // Toggle IsDown if the exiting
         // object has the "Cube" tag
         if (other.gameObject.tag == "Cube")
             IsDown = !IsDown;
     }
 }

Now, just attach the scripts appropriately and assign your triggers to the array on SlidingDoorListener. Hope that helps you out. I tried to put some explanatory comments in there. Hope I don't overload you.

Comment
Add comment · Show 5 · 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 watswat5 · Oct 08, 2013 at 01:26 AM 0
Share

Thanks! I was thinking of some kind of for loop as a solution a few $$anonymous$$utes ago. I just couldn't visualize how to break the loop if a trigger was deactivated!

avatar image TrickyHandz · Oct 08, 2013 at 01:29 AM 0
Share

There are several ways to do this. Since you said you were a beginner I thought I would do the most straight-forward approach without having to worry about breaking the loop. If it works for you, please mark the answer as accepted. Feel free to shoot me a message on the forums and I can show you some other approaches to this.

avatar image watswat5 · Oct 08, 2013 at 01:34 AM 0
Share

It worked after a couple modifications. In the door listener, I changed the else statement to else if(buttonsDown != buttons.Length && isOpen). This ensured that the closing animation played ONLY IF those two requirements were met. Otherwise, the close animation went on a loop endlessly.

avatar image watswat5 · Oct 08, 2013 at 01:35 AM 0
Share

Otherwise, from what I can tell, it works flawlessly!

avatar image TrickyHandz · Oct 08, 2013 at 01:49 AM 0
Share

Glad it helped you out.

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

16 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

Related Questions

Multiple Cars not working 1 Answer

C# For loop in button to set gameObjects in array to active 1 Answer

Distribute terrain in zones 3 Answers

How to make a or array of GUI.Button's? 1 Answer

Changing texture of pressed button in array 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