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);
}
}
sorry, I could not reply the other reply anymore, so I modified my reply.
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.
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
}
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"
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
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);
}
}
The reason is that you need to use an event to switch myBool between true and false.
@say_forever Isn't the $$anonymous$$ouse up setting the Bool true else false the event you mean?
Thanks I appreciate your help
~be