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 Jarod99 · Jul 15, 2016 at 03:19 AM · animationanimatorbooltoggle button

Animator boolean problem

Hello everyone.

I've looked around Google and the Unity forums for quite a while without finding an answer to my problem.

I am using the latest version of Unity, in a 2D project.

I am currently trying to access an Animator boolean from a script. When I click on the bool from the Animator window, it works normally, but I can't toggle it with the script for some reason. I have another Animator bool system for walking animations, and it works.

The boolean's name is "TextBox Active", I am trying to toggle it on and off with script. Pressing on the Space key should open the text box, and close it if it's already open.

Here's my script :

 using UnityEngine;
 using System.Collections;
 
 public class TextBoxAnimation : MonoBehaviour {
 
     public bool textBoxActive;
 
     void Start () 
     {
         textBoxActive = GameObject.Find("TextBox").GetComponent<Animator> ().GetBool ("TextBox Active");
     }
 
     void FixedUpdate () 
     {
         if (Input.GetKeyDown (KeyCode.Space)) 
         {
             textBoxActive = !textBoxActive;
         }
     }
 }

This is the last thing I tried out before asking for help here. I have also tried to go with : "public Animator textBoxActive;" and using .SetBool("TextBox Active", true) and .SetBool("TextBox Active", false), but it didn't work either.

This problem occurs every time I try to create a toggle. Sometimes, one part of the code will work, but the bool won't come back from its "inverted" position. I always use "bool = !bool;" whenever I can, but it doesn't work.

Since I'm a complete newcomer to C#, I know that I am doing something wrong and not seeing it. My code doesn't give me error messages in the console.

Thanks in advance for your help.

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

2 Replies

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

Answer by PhantomSarcasm · Jul 15, 2016 at 02:21 PM

I began to test your code and does not effectively work to me, I saw the most prudent, I would use two variables or just one depending on how comfortable you feel.

two var

     public Animator animacion;
     public bool dos;
     void Start () {
         animacion = GetComponent<Animator> ();
     }
     
     void Update () {
         if (Input.GetKeyDown (KeyCode.Space)) {
             dos = !dos;
             animacion.SetBool ("dos", dos);
         }
 
     
     }


with a var

     public bool dos;
 
     void Update () {
         if (Input.GetKeyDown (KeyCode.Space)) {
             dos = !dos;
             this.gameObject.GetComponent<Animator>().SetBool ("dos", dos);
         }
     }

alt text

I hope it is to your liking, and forgive if you were looking for the solution to your ccodigo without having to use another. sorry


captura-de-pantalla-12.png (17.4 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
1

Answer by Jarod99 · Jul 15, 2016 at 03:11 PM

@PhantomSarcasm I used your "two var" method on another script that wasn't working for the same reasons as my Animator one, and it works perfectly now.

I'll test it on a new Animator script that I'll be creating from scratch and I'll edit this post when I'm done testing. I will also be trying the "one var" method and check it out. Thanks for the help!

Edit : The "two var" method also worked on the Animator script, and it is now flawless. I used it with "OnTriggerStay2D" and "Update", and there is no problem with it. Thanks again for the help.

Comment
Add comment · Show 2 · 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 PhantomSarcasm · Jul 15, 2016 at 03:52 PM 0
Share

you're welcome. I'm glad that it has been helpful. luck with your test.

avatar image PhantomSarcasm · Jul 15, 2016 at 10:11 PM 0
Share

hahaha don´t worry, we are here to serve

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

110 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

Related Questions

PROBLEM WITH ANIM.SETBOOL NOT WORKING 0 Answers

Animation problem? 0 Answers

Can't play an animation from the Animator 1 Answer

stickman animation 1 Answer

Problems with using the same Animator Controller on multiple objects. 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