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 Bentoon · Nov 23, 2015 at 02:51 AM · c#animatorboolean

animator boolean

Hello I want to tigger a boolean to switch an animator state Obviously it's too late and I can't find the answer & am crosseyed with code:

 using UnityEngine;
 using System.Collections;
 
 public class mouseTrigger1 : MonoBehaviour {
 
 public Animator myAnimator;
     public bool myBool;
     
     void Start () {
         GetComponent<Animator> = myAnimator;
     }
 
 
     public void OnMouseUp()
     {
         if (myBool == "false") {     
             myAnimator.SetBool(myBool, true);
         } else
             myAnimator.SetBool(myBool, false);
     }
 }
 
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 say_forever · Nov 25, 2015 at 02:48 AM 0
Share

sorry, I could not reply the other reply anymore, so I modified my reply.

2 Replies

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

Answer by say_forever · Nov 23, 2015 at 09:21 AM

symbol "" mean type string, so you just need to remove it. it will work!

 using UnityEngine;
 using System.Collections;
 
 public class mouseTrigger1 : MonoBehaviour
 {
     public Animator myAnimator;
     public bool myBool = true; //recommend adding initial value
 
     void Start()
     {
         myAnimator = GetComponent<Animator>();
     }
 
     void OnMouseUp()
     {
         Debug.Log("M.U.");
         myBool = !myBool;
 
         myAnimator.applyRootMotion = myBool; // change here to make your script short and clear
         if (myBool)
         {
             Debug.Log("if is false");
         }
         else
         {
             Debug.Log("else");
         }
             
     }
 
 }

I am not sure what kind of stuff you want to change, so I use applyRootMotion to be example.

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 Bentoon · Dec 03, 2015 at 07:59 PM 0
Share

here's the javascript :

 #pragma strict
 
 var animator : Animator;
  var myBool : boolean;
  
 function Start () {
     animator = GetComponent("Animator");
 }
 
 
 function On$$anonymous$$ouseUp () {
 Debug.Log("$$anonymous$$O");
 
 
        if (myBool == false) {
       animator.SetBool("myBool", true);
       Debug.Log("SetBool");
       }
       else {
       //if (myBool == true){
       animator.SetBool("myBool", false);
       Debug.Log("SetBoolFalse");
       }
       
      myBool = !myBool;  // add this line
      
       }
 
avatar image
0

Answer by Bentoon · Nov 23, 2015 at 11:39 AM

Thanks @say_forever But I m still getting errors

for:

     GetComponent<Animator> = myAnimator;

Assets/Scripts/mouseTrigger1.cs(10,17): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer

&

     if (myBool == false) {     
         myAnimator.SetBool(myBool, true);

Assets/Scripts/mouseTrigger1.cs(17,36): error CS1502: The best overloaded method match for `UnityEngine.Animator.SetBool(string, bool)' has some invalid arguments

&

         myAnimator.SetBool(myBool, false);

also gets this error:

Cannot connect "bool" expression to type "string"

Comment
Add comment · Show 6 · 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 say_forever · Nov 23, 2015 at 12:42 PM 0
Share

when you stay your mouse on error line, it will tell you what is wrong with it. GetComponent = myAnimator; the left hand side of an assignment must be a variable property or indexer it means you can't assignment custom Animator to it.

finally, when you stay your mouse on function, it will also tell you how to use it. SetBool method need the first parameter be string, so you need add ""symbol

avatar image Bentoon · Nov 23, 2015 at 07:02 PM 0
Share

Thanks @say_forever

Appreciate it!

I get no errors but it never triggers the else using System.Collections;

 public class mouseTrigger1 : $$anonymous$$onoBehaviour {
 
 public Animator myAnimator;
 public bool myBool;
     
     void Start () {
         myAnimator = GetComponent<Animator>();
     }
 
 
     public void On$$anonymous$$ouseUp()
     {
         Debug.Log("$$anonymous$$.U.");
         if (myBool == false) {
             Debug.Log("if is false");
             myAnimator.SetBool("myBool", true);
         } else
             Debug.Log("else");
             myAnimator.SetBool("myBool", false);
     }
 
 
 }
 
avatar image say_forever Bentoon · Nov 24, 2015 at 02:17 AM 0
Share

The reason is that you need to use an event to switch myBool between true and false.

avatar image Bentoon say_forever · Nov 24, 2015 at 03:54 AM 0
Share

@say_forever Isn't the $$anonymous$$ouse up setting the Bool true else false the event you mean?

Thanks I appreciate your help

~be

Show more comments
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

35 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

Related Questions

Script Variable not transfering 0 Answers

error CS0266 Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 2 Answers

OnMouseExit triggering when its not supposed to 1 Answer

how do i check if all booleans in an array are false? C# 2 Answers

Automatic Switching of two or more booleans in Inspector 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