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 MerlinsMaster · Dec 23, 2015 at 08:54 AM · variablescallingother scriptfrom

Issue calling a variable from another script - C#

Hey guys,

I have a situation in my game in which a button needs to be pressed to open a door.

For the button, the actual button object and the base are parented under an empty gameobject called ButtonParent, to which the following script is attached:

 using UnityEngine;
 using System.Collections;
 
 public class ButtonScript : MonoBehaviour 
 {
 
     Animator animator;
     public bool buttonDown;
     public bool buttonInRange;
 
     void Start()
     {
         buttonDown = false;
         animator = GetComponent<Animator>();
     }
 
     void Update()
     {
         if (Input.GetButtonDown("Jump") && buttonInRange)
         {
             if (!buttonDown)
             {
                 ButtonControl("Open");
                 buttonDown = true;
             }
             else
             {
                 ButtonControl("Close");
                 buttonDown = false;
             }
         }
     }
 
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "Player")
         {
              buttonInRange = true;
         }
     }
 
     void OnTriggerExit(Collider col)
     {
         if (col.gameObject.tag == "Player")
         {
             buttonInRange = false;
         }
     }
 
     void ButtonControl(string direction)
     {
         animator.SetTrigger(direction);
     }
 
 }

For the door, the two door objects are parented under an empty gameobject called DoorParent, to which the following script is attached:

 using UnityEngine;
 using System.Collections;
 
 public class Doors : MonoBehaviour
 {
     public ButtonScript refButtonScript;
     Animator animator;
     bool doorOpen;
         
     void Start()
     {
         doorOpen = false;
         animator = GetComponent<Animator>();
         refButtonScript = FindObjectOfType<ButtonScript>(); 
     }
 
     void Update()
     {
       
 
        if (refButtonScript.buttonDown == true)
         {
             DoorControl("Open");
             doorOpen = true;
             Debug.Log("Door should be open");
         }
 
         if (refButtonScript.buttonDown == false)
         {
             DoorControl("Close");
             doorOpen = false;
             Debug.Log("Door should be closed");
         }
 
     }
 
     
     void DoorControl(string direction)
     {
         animator.SetTrigger(direction);
     }
 
 }

What's supposed to happen is, when the player is within reach of the button, pressing space pushes the button, causing the bool buttonDown to be true, which is then called from the door script to open the doors.

I got it to work, almost.

When the button is pressed, the doors open, but then they close and open again. Likewise, when the button is pressed again, the doors close, and then open and close again.

I can't figure out for the life of me why the doors are opening and closing twice. Can anyone tell what is going wrong?

Thanks.

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
2

Answer by allenallenallen · Dec 23, 2015 at 09:42 AM

I think the mistake you made here is in the Door script. In the Door script, you're constantly checking whether the door button has been pushed or not, which is a redundant action. Because if a button is pressed, the door MUST react.

Instead of Door script containing a reference to the Button script, have the Button script contain a reference to the Door script. Doors open or close depending on the Buttons, not the other way around.

For the Button script:

 // Add the reference variable for the Doors script.
 public Doors refDoors;
 
 void Awake(){
     refDoors = FindObjectOfType<Doors>();  // To be honest, I wouldn't use this method to get the Doors script. What if you have more than one Doors scripts in the scene?
 }
 
  void Update()
      {
          if (Input.GetButtonDown("Jump") && buttonInRange)
          {
              if (!buttonDown)
              {
                  ButtonControl("Open");
                  buttonDown = true;
                  refDoors.OpenDoors();  // Calls a void in the Doors script that opens the doors.
              }
              else
              {
                  ButtonControl("Close");
                  buttonDown = false;
                  refDoors.CloseDoors();  // Calls a void in the Doors script that closes the doors.
              }
          }
      }

For the Doors script:

 // Delete everything in void Update() but add the following voids.

 // Open doors
 void OpenDoors(){
     DoorControl("Open");
     doorOpen = true;
     Debug.Log("Door should be open");
 }

 // Close doors
 void CloseDoors(){
     DoorControl("Close");
     doorOpen = false;
     Debug.Log("Door should be closed");
 }


And now the Doors script doesn't need to waste time and resources checking every frame to see if the buttons are pushed or not. I believe this is a much more effective way than what you have right now.

Comment
Add comment · Show 4 · 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 MerlinsMaster · Dec 23, 2015 at 10:57 AM 0
Share

Thanks. That solution worked.

I do have a question about that one comment you had:

refDoors = FindObjectOfType(); // To be honest, I wouldn't use this method to get the Doors script. What if you have more than one Doors scripts in the scene?

I understand your concern there. Unfortunately, all of the tutorials I was able to find explaining the other methods on how to call a variable from another script predate the launch of Unity 5, so the code in those tutorials no longer work.

For instance, I was trying to do it this way, with ScriptB calling info from ScriptA:

 using UnityEngine;
 using System.Collections;
 
 public class scriptA : $$anonymous$$onoBehaviour 
 {
         public scriptB _scriptB;
 
     void Start ()
     {
         _scriptB.helloScriptB();
     }
 }

 and


 using UnityEngine;
 using System.Collections;
 
 public class scriptB : $$anonymous$$onoBehaviour 
 {
         public void helloScriptB ()
     {
         print("hello, I am script b");
     }
 }

I can't figure out what coding change I need to make to make this work. If you could explain to me how to do it this way in Unity 5, you'd really be doing me a solid.

avatar image allenallenallen MerlinsMaster · Dec 23, 2015 at 11:37 AM 1
Share

I guess you dragged the scriptB into the scriptA variable in the editor, right?

That's one way to do it. You'll always have the correct script if you manually drag and choose the script.

avatar image MerlinsMaster allenallenallen · Dec 23, 2015 at 11:43 AM 0
Share

Actually, it doesn't allow me to drag the script in. But if it's done properly, it should just grab the script on its own, yes?

Show more comments

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

31 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

Related Questions

How do you return variables from other scripts? 2 Answers

Calling variables from other scripts 2 Answers

Getter function not returning correct value from another script (C# scripting) 1 Answer

Calling a variable from another script overwrites it? 1 Answer

How can I take a variable from another script and then apply it to a newly insantiated prefab? 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