Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Statemachine assigning variables?

Discussion in 'Scripting' started by Resilo, Dec 14, 2017.

  1. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    ok so i have a monobehaviour called battle
    and i have a statemachinebehaviour called attack
    the script attack has a variable gamobject called player
    the script battle has an animator called anim

    i have already tried
    anim.GetBehaviour<Attack>().player = this.gameobject

    i also tried adding at state enter
    player = animator.gameobject

    and no results

    what is the best way to make the variable player the gameobject which the animator is attached to?
     
    Last edited: Dec 14, 2017
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    The easiest thing to do when you are getting started is to make a public variable and then drag the game object onto it in the editor.

    Be careful using a variable like gameobject because there is a default named gameObject that the script is attached. If you want that gameObject, you can use this.gameObject, or just gameObject. If you want a game object other than the default, then name it something else like public GameObject enemy and drag it onto the name in the editor.

    Really, though, it sounds like you might want to do a few more tutorials in the learn section.
     
  3. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    thanks for replying but dragging and dropping into a public variable because StateMachineBehaviour deals with scene objects and is relevant to the scene unlike a monobehaviour which can use asset objects(dragging and droping)


    heres both scripts in case they help anyone out further


    statemachine
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Attack : StateMachineBehaviour
    5. {
    6.     public Battle battler;
    7.     public GameObject player;
    8.     public Inputattack attack;
    9.  
    10.     // This will be called when the animator first transitions to this state.
    11.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    12.     {
    13.         attack = player.GetComponent<Inputattack>();
    14.         battler = player.GetComponent<Battle>();
    15.         battler.startattack = true;
    16.         battler.startattacktwo = true;
    17.         attack.set = true;
    18.  
    19.  
    20.     }
    21.     // This will be called when the animator first transitions to this state.
    22.     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    23.     {
    24.         battler = player.GetComponent<Battle>();
    25.         battler.startattack = false;
    26.         battler.startattacktwo = false;
    27.         attack.settwo = true;
    28.     }
    29. }

    NetworkBehaviour script



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class Battle : NetworkBehaviour {
    7.     [SyncVar]
    8.     public int battlenumber;
    9.     public Transform enemy;
    10.     [SyncVar]
    11.     public bool blocked;
    12.     public Animator anim;
    13.     public Animator playeranim;
    14.     public bool noshield;
    15.     public float weightdifference;
    16.     [SyncVar]
    17.     public bool canbeknockeddown;
    18.     [SyncVar]
    19.     public bool knockingdown;
    20.     [SyncVar]
    21.     public bool startattack;
    22.     [SyncVar]
    23.     public bool startattacktwo;
    24.     public GameObject attacker;
    25.     public GameObject player;
    26.     public StateMachineBehaviour attackstate;
    27.  
    28.     [ClientRpc]
    29.     void Rpc_getdamaged(string trigger)
    30.     {
    31.      
    32.         anim.SetTrigger(trigger);
    33.     }
    34.  
    35.  
    36.  
    37.     public void gethit()
    38.     {
    39.         if (isServer)
    40.         {
    41.  
    42.                 GetComponent<Stats>().hp = Mathf.Clamp(GetComponent<Stats>().hp - attacker.GetComponent<Stats>().damage, 0, GetComponent<Stats>().maxhp);
    43.                 anim.SetTrigger("gethit");
    44.                 Rpc_getdamaged("gethit");
    45.  
    46.                 if (GetComponent<Stats>().hp == 0)
    47.                 {
    48.  
    49.                     death();
    50.                 }
    51.             attacker = null;
    52.            
    53.         }
    54.     }
    55.  
    56.  
    57.    void death()
    58.     {
    59.         if (isServer)
    60.         {
    61.             GetComponent<Stats>().playeraudio.clip = GetComponent<Stats>().death;
    62.             GetComponent<Stats>().playeraudio.Play();
    63.             if (transform.root.tag == "Enemy")
    64.             {
    65.  
    66.                 foreach (GameObject player in GetComponent<Stats>().attackerslist)
    67.                 {
    68.                     player.GetComponent<Stats>().xp += GetComponent<Stats>().xpgain;
    69.                 }
    70.                 GetComponent<myspawnobject>().spawner.GetComponent<Enemyspawn>().enemies -= 1;
    71.                 Destroy(gameObject);
    72.  
    73.  
    74.             }
    75.             if (transform.tag == "player")
    76.             {
    77.  
    78.                 GetComponent<Stats>().dead = true;
    79.  
    80.             }
    81.         }
    82.     }
    83.  
    84.  
    85.  
    86.  
    87.  
    88.  
    89.  
    90.  
    91.  
    92.  
    93.  
    94.  
    95.  
    96.  
    97.  
    98.  
    99.  
    100.  
    101.  
    102.  
    103.  
    104.  
    105.  
    106.  
    107.  
    108.  
    109.  
    110.  
    111.     IEnumerator knockeddown()
    112.     {
    113.  
    114.         enemy.transform.root.GetComponent<Battle>().canbeknockeddown = true;
    115.         enemy.transform.root.GetComponent<Battle>().knockingdown = true;
    116.  
    117.  
    118.         yield return new WaitForSeconds(7 * Time.deltaTime);
    119.         enemy.transform.root.GetComponent<Battle>().canbeknockeddown = false;
    120.         enemy.transform.root.GetComponent<Battle>().knockingdown = false;
    121.  
    122.     }
    123.  
    124.  
    125.  
    126.  
    127.  
    128.  
    129.  
    130.     // Use this for initialization
    131.     void Start () {
    132.  
    133.  
    134.  
    135.     }
    136.  
    137.     // Update is called once per frame
    138.  
    139.     void Update () {
    140.  
    141.         if(anim == null)
    142.         {
    143.             anim = GetComponent<Animator>();
    144.         }
    145.  
    146.         if (anim != null)
    147.             {
    148.             if(anim.GetBehaviour<Attack>().player == null)
    149.             {
    150.                 anim.GetBehaviour<Attack>().player = player;
    151.             }
    152.         }
    153.  
    154.  
    155.  
    156.     }
    157. }
    158.  
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Did you get any errors, or did nothing happen? That doesn't sound right.
     
  5. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    checked the editor server side and player side there is no difference and player is never set even thought it clearly state in update that it should be
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I don't see where the player variable in Network was ever assigned anything. It looks to me like you are assigning a null to a null.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    So, hopefully I got what you're trying to do. I would say maybe try this in Start().
    Code (csharp):
    1.  
    2. anim = GetComponent<Animator>();
    3. anim.GetBehaviour<Attack>().player = gameObject;
    4.  
    Just see if you get any errors.
     
  8. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    The player variable in battle is assigned by dragging and dropping
     
  9. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Then you have it reversed.
    anim.GetBehaviour<Attack>().player = player;

    should be
    player = anim.GetBehaviour<Attack>().player;

    I have no idea why you would do that, but the unassigned goes on the left and you've already assigned player in the other script.
     
  10. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    tried that methos the variable still did not get assigned

    the player variable in the script attack is the one that is null and is not getting assigned
    the variable player in battle is already assigned if i put
    player = anim.GetBehaviour<Attack>().player;
    i would be feeding the variable player from the script attack to the script battle
    which would result in null because the player variable in attack is not getting assigned correctly
     
  11. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Then get the player from battle and assign it to the player in attack. If the player in attack is null, you are assigning a null to a null, because the player in Network is null.
     
    Last edited: Dec 14, 2017
  12. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    so basically what your saying is the way i am sending the variable to the statemachinebehaviour is correct with
    anim.GetBehaviour<Attack>().player = (insertvariablehere)
     
  13. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It should be. If you have code completion, then you should get the player option after the period. But yes, it's public, so you should be able to assign it from anywhere with an already assigned variable.
     
  14. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    so for anyone wondering this is a bug that it will not show up in the inspector and was fixed in 2017.1 problem being my unity is 5.6.3 time to update
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah well, good you worked out the problem! =)