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 MIkeHewson86 · Oct 13, 2016 at 10:54 AM · animatorbooleanenabled

how to turn animator on if bool = true

hi i am having trouble trying to get my door open animator to work . i would like the animator component to be off on start and to come on when the bool chestOpened is true. i am coding in c# i just want a bit of code to say if bool == false animator not active if bool is true turn on animator (see picture for what i would like to control from script)

any help would be appreciated. alt text

animator-onoff.jpeg (204.0 kB)
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 Sergio7888 · Oct 13, 2016 at 04:50 PM 0
Share

Use animator.enabled=true;

2 Replies

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

Answer by MIkeHewson86 · Oct 14, 2016 at 11:17 AM

I found out what my problem was. i wanted the door open animation in the animator to play when the bool chestOpened is true. I had not set up the animator properly. this tutorial helpped with that: https://www.youtube.com/watch?v=M1bEtHOE8z8

basically i created 2 animations in the animator the first one was doorClosed the second was door opening.

i created a bool in the animator called openDoor.

i set the doorClosed animation to default then created a transition from doorClosed to doorOpened. and in the inspector within the transition i added the condition that of openDoor is true then transition to doorOpen animation.

alt text

alt text

then i wrote a simple script as follows: using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;

 public class openDoor : MonoBehaviour {
 
     public ChestKey chestKey;
 
     public Animator anim;
 
     public bool doorOpen;
 
     // Use this for initialization
     void Start ()
     {
         chestKey = GameObject.Find("Chest").GetComponent<ChestKey>();
 
         anim = GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update ()
     {
         anim.SetBool("doorOpen", doorOpen);
 
         if (chestKey.chestOpened)
         {
             doorOpen = true;
             Debug.Log("door is open");
         }
         if (!chestKey.chestOpened)
         {
             doorOpen = false;
         }
     }
 }
 

animator1.png (150.3 kB)
animatorconditions.png (178.6 kB)
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
avatar image
0

Answer by Zahiro · Oct 14, 2016 at 11:17 AM

Probably the easiest way you can do this is the way you enable and disable any other of the components , with GetComponent<>().enabled

So your script will look like this :

 void Update () {
         if (myBool) {
             GetComponent<Animator> ().enabled = true;
         }
     }

Keep in mind that you should place this code on the same gameObject that the animator is

Comment
Add comment · Show 1 · 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 Zahiro · Oct 14, 2016 at 10:31 AM 0
Share

You can also disable the door collider (if you have one) by adding GetComponent<BoxCollider> ().enabled = false; right after GetComponent<Animator> ().enabled = true;

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

Make a boolean positive and then negative with same button 1 Answer

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

How to check how many times an animation has played? 1 Answer

Add a bool to animator & activate by key press 1 Answer

how do i turn animator on from script if bool is true 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