Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 $$anonymous$$ · Feb 06, 2015 at 05:33 PM · animationvariablesbooleanif else

Make condition for opening a door (can only be opened when a lever has been activated before)

Hey guys, I'm the basic Unity noob but have to finish a project for school. It's a small game level and what I want to do is to enter a house that I built in Maya. Second thing is that you should only be able to open the door when you first activated a lever. The house is a small cabin with an animation on the door, so it should open when you click the door. The lever also has a small animation that is triggered by clicking on it. Both of it already works with this code:

function Start ()

{

 animation.Stop();

}

function Update ()

{

if (Input.GetButtonDown("Fire1")) //check to see if the left mouse was pushed.

 {

    animation.CrossFade ("OpenDoor"); // if pushed play the animation

 }

}

So far so good. Now I have this problem: How can I make the animation only play when the lever is already activated (or in other words when that animation has already played) and otherwise it would give some onscreen text like "the door is locked..." I tried with a boolean now, like leverActivated = true when the animation has played, but I don't get how I can use that boolean in the script for the door now...

I would very much appreciate your help and sorry if I'm asking dumb questions...

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
0

Answer by Phillipus · Feb 06, 2015 at 07:53 PM

Try using public variables and changing them from other scripts, e.g. when the lever is activated, it calls a public function in the door's script that sets a boolean as true.

Example (its in C#):

Lever script:

 Gameobject door;
 
 void Start() {
     door = Gameobject.Find("Door")
 }
 
 void ActivateLever() {
     door.GetComponent<Script>().SetOpenable(true);
 }

Door Script:

 bool openable;
 
 void Start() {
     animation.Stop();
     openable = false;
 }
 
 void Update() {
     if(Input.GetButtonDown("Fire1") && openable){
         animation.CrossFade ("OpenDoor");
     } else {
         // do something for if the lever isn't pushed
     }
 }
 
 public void SetOpenable(boolean set) {
     openable = set;
 }

You would change Door in Gameobject.Find("Door") to the name of the door, and Script in `GetComponent

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

2 People are following this question.

avatar image avatar image

Related Questions

Does an animation continuously set it's frame values? 1 Answer

Blend Tree Transition Conditions. 0 Answers

Stop Animation State in Animator from Speeding Up after Playing it a Second Time? 0 Answers

Animations in booleans loop? 1 Answer

making variables work in two scenes 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