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 /
  • Help Room /
avatar image
0
Question by Zix_Serro · Sep 16, 2017 at 04:11 PM · animationspritesequence

Having scripts affect each other in sequence

I'm working on a 2D game where there are door-like walls that can be opened by pressing a switch. I wanted these doors to be set up as sprites stacked up on one another; that way I can make each door as tall or small as they need to be. However, I'm having problems with the doors opening correctly. I wanted them to open from the bottom up, and have the switch set to only open the bottom segment of the door; however, when I press the switch, all segments of the door open at the same time. Also, when I come off the switch, it should close the door, but it doesn't. The door stays open. How do I get the door to open from the bottom up, and close when the switch isn't being pressed down?

Here's the code for the door:
public class LockedWall : MonoBehaviour {

     public Animator anim;
     private Collider2D collision;
     public GameObject segmentAbove;
     public GameObject segmentBelow;
     public bool isBottom;
     public bool isTop;
     public bool isOpen;
 
     void Start()
     {
         anim = GetComponent<Animator>();
         collision = GetComponent<Collider2D>();
         isOpen = false;
         if(segmentBelow.tag != "LockedWall")
         {
             isBottom = true;
         }
         if (segmentAbove.tag != "LockedWall")
         {
             isTop = true;
         }
         if (segmentBelow.tag == "LockedWall")
         {
             isBottom = false;
             segmentBelow.GetComponent<LockedWall>();
         }
         if (segmentAbove.tag == "LockedWall")
         {
             isTop = false;
             segmentBelow.GetComponent<LockedWall>();
         }
     }
 
     public void DoorBottomOpens()
     {
         if (isBottom)
         {
             anim.SetBool("open", true);
             collision.enabled = false;
             segmentAbove.GetComponent<LockedWall>().DoorOpens();
             IsOpen();
         }
     }
 
     public void DoorOpens()
     {
         if (!isBottom)
         {
             anim.SetBool("open", true);
             collision.enabled = false;
             IsOpen();
         }
     }
 
     public void DoorTopCloses()
     {
         if (isTop)
         {
             anim.SetBool("open", false);
             collision.enabled = true;
             segmentBelow.GetComponent<LockedWall>().DoorCloses();
             IsClosed();
         }
     }
 
     public void DoorCloses()
     {
         if (!isTop && segmentAbove.GetComponent<LockedWall>().isOpen == false)
         {
             anim.SetBool("open", false);
             collision.enabled = true;
             IsClosed();
         }
     }
 
     void IsOpen()
     {
         if (!isTop)
         {
             segmentAbove.GetComponent<LockedWall>().DoorOpens();
         }
     }
 
     void IsClosed()
     {
         if (!isBottom)
         {
             segmentBelow.GetComponent<LockedWall>().DoorCloses();
         }
     }
 }

And the script for the switch:

 public class Switch : MonoBehaviour {
 
     private Animator anim;
     [SerializeField]
     private bool redSwitch;
     [SerializeField]
     private GameObject doorTrig;
 
     void Start()
     {
         anim = GetComponent<Animator>();
     }
 
     void OnTriggerStay2D()
     {
         anim.SetBool("down", true);
         doorTrig.GetComponent<LockedWall>().DoorBottomOpens();
     }
 
     void OnTriggerExit2D()
     {
         if (!redSwitch)
         {
             return;
         }
         anim.SetBool("down", false);
         doorTrig.GetComponent<LockedWall>().DoorTopCloses();
     }
 }
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

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

233 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

Related Questions

Sprite animation cannot be play when player retry Unity 0 Answers

Syncing 2D Sprite Animation 0 Answers

How to do these energy spheres 0 Answers

Spriter 2D animation to Unity 1 Answer

How do I display UI animation? 0 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