Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Schytheron · Dec 22, 2015 at 08:37 PM · scripting problemcolliderbuttonvariablefindgameobjectswithtag

Variable only changes for one object that has the script attached and not the other objects with the same script.

I have 3 big buttons in my scene that are placed on the floor. Each button is a prefab and therefore the same. Each button has a script that checks if something collides with the button. I then have a door with a script attached called ButtonChecker which checks if all buttons are colliding with something. If they are, the door should spin around (its a spinning door).

Here is the script thats attached to each button:

  public class ButtonPress : MonoBehaviour
  {
  
      //public GameObject TurningDoor;
      //float hoverForce = 0.2f;
      //bool CollidingObject = false;
      public int activeButtons = 0;
  
      // Use this for initialization
      void Start ()
      {
      
      }
      
      // Update is called once per frame
      void Update ()
      {
  
      }
  
      void OnCollisionEnter (Collision Other)
      {
          //CollidingObject = true;
          activeButtons++;
  
      }
  
      void OnCollisionExit (Collision Other) {
          //CollidingObject = false;
          activeButtons--;
  
      }

And here is the script that is attached to the door:

 public class ButtonChecker : MonoBehaviour {
      public int amountofButtons;
      public GameObject FirstButton;
      public ButtonPress ButtonPresser;
      public GameObject[] buttons;
      float rotationForce = 0.1f;
  
      void Start() {
          ButtonPresser = FirstButton.GetComponent<ButtonPress> ();
  
  
          buttons = GameObject.FindGameObjectsWithTag("Button");
          amountofButtons = buttons.Length;
      }
      
      // Update is called once per frame
      void Update () {
          Debug.Log (buttons.Length);
          if (ButtonPresser.activeButtons == amountofButtons) {
              transform.Rotate( new Vector3(0, 5, 0) * rotationForce, Space.Self );
          } 
  
  
      }
  }

The problem is that the Int "activeButtons" in the ButtonPress script is only adds for one button. Example: If I stand on Button 1, activebuttons value will be set to 1. If I then put a cube on Button 2 (while standing on Button 1) , activeButtons value wont change to 2.

Comment
Add comment · Show 1
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 wibble82 · Dec 22, 2015 at 04:25 PM 0
Share

Cool - waiting for it to be moderated before I can answer :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Schytheron · Dec 23, 2015 at 04:32 PM

I solved it myself! I just had to reverse my logic. Instead of having a activeButtons int in the ButtonPress script I put it in the ButtonChecker script instead and let the ButtonPress script access to the ButtonChecker Script and let it change the activeButton variable in the ButtonChecker script when colliding with a button. TADA!

Now the ButtonPress script looks like this:

 using UnityEngine;
 using System.Collections;
 
 
 public class ButtonPress : MonoBehaviour
 {
 
     //public GameObject TurningDoor;
     //float hoverForce = 0.2f;
     //bool CollidingObject = false;
     public ButtonChecker ButtonCheck;
     public GameObject Door;
 
     // Use this for initialization
     void Start ()
     {
         ButtonCheck = Door.GetComponent<ButtonChecker> ();
     
     }
     
     // Update is called once per frame
     void Update ()
     {
 
     }
 
     void OnCollisionEnter (Collision Other)
     {
         //CollidingObject = true;
         ButtonCheck.activeButtons++;
     }
 
     void OnCollisionExit (Collision Other) {
         //CollidingObject = false;
         ButtonCheck.activeButtons--;
     }

And the ButtonChecker Script looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class ButtonChecker : MonoBehaviour {
     public int amountofButtons;
     public int activeButtons = 0;
     public GameObject[] buttons;
     float rotationForce = 0.1f;
 
     void Start() {
 
         buttons = GameObject.FindGameObjectsWithTag("Button");
         amountofButtons = buttons.Length;
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log (activeButtons);
         if (activeButtons == amountofButtons) {
             transform.Rotate( new Vector3(0, 5, 0) * rotationForce, Space.Self );
         } 
 
 
     }
 }
 

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I need my play button to load level 1 when the player touches the play button 2 Answers

Change Material depending on Collider position? 2 Answers

pubblic variable don't get change from script with OnTriggerStay 1 Answer

GetMouseButtonDown From a close distant 1 Answer

How to change between scripts on trigger? 2 Answers


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