Search Unity

Hot to play AnimatorBool on trigger

Discussion in 'Scripting' started by staincorb, Jul 26, 2018.

  1. staincorb

    staincorb

    Joined:
    Mar 30, 2009
    Posts:
    123
    Hello all.

    I am currently creating a character animation system with mecanim, (Perfect for non programmers).

    The thing is that I am able to animate character with input perfectly fine.

    But I cant seam to find a script working for playing a "Bool Parameter" on trigger enter, I have tried what I found in JS and C# but I am not programmer and I rely on copy paste and basic code adaptation. (Mind set is not for coding or grammar :)

    ////////////////////////////////Code on floor trigger///////////////////////////////////////// JS
    private var lands = false;
    var ObjectToPlay : GameObject;
    function OnTriggerEnter()
    {
    if (!lands)
    {
    ObjectToPlay.GetComponent.Animator.SetBool("Land");
    lands = true;
    }
    }

    ////////////////////////////////Code on object that exists on collide///////////////////////////////////////// CS

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class ActivateState_Land : MonoBehaviour
    {

    Animator m_animator;

    void Start()
    {
    m_animator = GetComponent<Animator>();
    }
    void Update()
    {

    m_animator.SetBool("Land", true);


    }
    }
     

    Attached Files:

  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Hi Staincorb,
    First you should only use 1 scripting language in your game. I would use C#, so don't look at java script.

    I think you're using java. that code has the OnTrigger in it.

    This is piece that is wrong
    ObjectToPlay.GetComponent.Animator.SetBool("Land");

    it should be
    ObjectToPlay.GetComponent.Animator.SetBool("Land", lands);


    Code (CSharp):
    1. if (!lands) //if lands is false, then do this
    2. {
    3. ObjectToPlay.GetComponent.Animator.SetBool("Land", lands); //set the bool in the animation to false,
    4. lands = true;  //set the boot to true, so that we don't play the animation multiple times
    5. }
     
    Last edited: Jul 26, 2018
  3. staincorb

    staincorb

    Joined:
    Mar 30, 2009
    Posts:
    123
    Thanks a lot, I changed that code and still does not work I get this message, "NullReferenceExeption: Object reference not set to an instance of an object."

    Also its a java that I have it written in,
    I tried making this same code into C# but code would not let me run game so I trashed it.
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Sorry for the delay, had to get to work.
    I've looked closer at the code. and converted it to C#. Your code never set the ObjectToPlay variable, so that was your Null ref
    And this script needs to be attached to the gameObject that has the Animator.

    Code (CSharp):
    1. public bool lands = false;
    2.     private GameObject ObjectToPlay;
    3.  
    4.     void Awake()
    5.     {
    6.         ObjectToPlay = this.gameObject;
    7.     }
    8.  
    9.     void OnTriggerEnter()
    10.     {
    11.         if (!lands)
    12.         {
    13.             ObjectToPlay.GetComponent<Animator>().SetBool("Land", true);
    14.             lands = true;
    15.         }
    16.     }
     
  5. staincorb

    staincorb

    Joined:
    Mar 30, 2009
    Posts:
    123
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class OnCollide_PlayState : MonoBehaviour
    {


    public bool lands = false;
    private GameObject ObjectToPlay;

    void Awake()
    {
    ObjectToPlay = this.gameObject;
    }

    void OnTriggerEnter()
    {
    if (!lands)
    {
    ObjectToPlay.GetComponent<Animator>().SetBool("Land", true);
    lands = true;
    }
    }
    }


    This is the code I am using and I attached it to the game object with animator, it does not seem to do anything.

    Code is a tricky thing.
     
  6. staincorb

    staincorb

    Joined:
    Mar 30, 2009
    Posts:
    123
    Hey johne

    Thank you for all your help.
    The code is not working and I think its counter Producing to get it work at the moment since its not the way I need it, I want to thank you for your support, I am hiring a coder to help me with these simple things at the moment.

    And this board can be closed also :)
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    @staincorb the code is only setting the bool in the Animator when the player collides with the trigger object.
    It's sound like you're not getting any errors.
    When you play the scene. Have the Animator open, so you can see the Bool switch from "unchecked" to "checked". the bool should be called "Land"
    if you see the bool changing state. then you have to build the transitions.

    I'd be glad to help out on any programming needs you might have.
     
  8. staincorb

    staincorb

    Joined:
    Mar 30, 2009
    Posts:
    123
    The bool is not changing state.
     
  9. staincorb

    staincorb

    Joined:
    Mar 30, 2009
    Posts:
    123
    Can you send me your mail so I can write to you. :)
     
    Last edited: Jul 27, 2018
  10. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Have you verified that the OnTriggerEnter() is running?
    Code (CSharp):
    1. void OnTriggerEnter()
    2. {
    3.     Debug.Log("OnTriggerEnter has ran");
    4.    
    5.     if (!lands)
    6.     {
    7.         Debug.Log("Set the bool has ran");
    8.         ObjectToPlay.GetComponent<Animator>().SetBool("Land", true);
    9.         lands = true;
    10.     }
    11. }
    12. }