Search Unity

Question How do I access the Animator component in this case?

Discussion in 'Scripting' started by unity_B3D5697DFA04615A0786, Sep 26, 2022.

  1. unity_B3D5697DFA04615A0786

    unity_B3D5697DFA04615A0786

    Joined:
    Jun 11, 2021
    Posts:
    25
    My problem is the following, I have my player, which was a capsule, it works, but inside I add my 3d model, and inside the 3d model the animator, now, the parent component "Player" that contains Rigidbody, charactercontroller, my c#, etc , can't access the animator
    upload_2022-9-26_13-45-15.png
    Player:
    upload_2022-9-26_13-47-1.png
    Mario (Model 3d)
    upload_2022-9-26_13-47-26.png
    Right now, Mario has an idle animation, I wanted him to walk, but I need to access this component (Inside the player).
    I don't know if this is the correct way to do it. upload_2022-9-26_13-45-15.png upload_2022-9-26_13-47-1.png upload_2022-9-26_13-47-26.png upload_2022-9-26_13-45-15.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Make a public Animator field, drag it in, just like 100% of everything else in Unity.

    Referencing variables, fields, methods (anything non-static) in other script instances (or on other GameObjects):

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
  3. unity_B3D5697DFA04615A0786

    unity_B3D5697DFA04615A0786

    Joined:
    Jun 11, 2021
    Posts:
    25
    I already tried it, and got the error:
    MissingComponentException: There is no 'Animator' attached to the "Player" game object, but a script is trying to access it.
    You probably need to add a Animator to the game object "Player". Or your script needs to check if the component is attached before using it.
    UnityEngine.Animator.SetBool (System.String name, System.Boolean value) (at <48d00b1828004564a4e4431fc5279bdf>:0)
    Player_Mov.Update () (at Assets/Scripts/Player_Mov.cs:45)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Then you didn't do it right!

    The above error messages suggests it looked on
    Player
    , but you have explained that it is on
    Mario
    .

    Restart your investigation there.

    Remember, this is just a nullref, something that should NEVER require a forum post.

    It's always the same three steps... always.

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <---- I suspect this part has not even been completed successfully
    - Identify why it is null
    - Fix that
     
  5. unity_B3D5697DFA04615A0786

    unity_B3D5697DFA04615A0786

    Joined:
    Jun 11, 2021
    Posts:
    25
    But, I'm not that stupid... I made Animator as public, and I dragged Mario (WHICH HAS THE ANIMATOR)
     
  6. unity_B3D5697DFA04615A0786

    unity_B3D5697DFA04615A0786

    Joined:
    Jun 11, 2021
    Posts:
    25
    I asked something because I'm trying to learn, and the only answer seems to be that he made fun of me, taking me for an idiot, and then he wasn't even helpful.
     
    jhughes9_unity likes this.
  7. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,648
    What does your script look like?

    It should have this in your class
    Code (CSharp):
    1. public Animator animatorVariableName;
    and then when you do that, it creates a field in the inspector for your component. Then you just drag the component into the field.
    Then, later on when you want to do something with it, you access it like this.
    Code (CSharp):
    1. animatorVariableName.DoSomething();
     
  8. unity_B3D5697DFA04615A0786

    unity_B3D5697DFA04615A0786

    Joined:
    Jun 11, 2021
    Posts:
    25
    is exactly how I have it since today, In Player I have all the components (rb, charactercontroller, capsule, c#) and inside also "mario" as seen in the image, but when I drag the controller from "Mario" to "Player" I get gives that error, that the user above took me for a ride
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    You haven't shown your code at all, so we can only assume. Something in your code could be dereferencing the component, or you have errant instance of this component somewhere else in your scene that's throwing this error.

    Error messages generally never lie. If something is saying it's missing a reference, then it's 100% true. You just need to figure out why.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Based on your public hallucination record, which I will copy here for future reference:

    I'm afraid I cannot help you any further.

    This is software engineering and everything I posted above has stood the test of time and DECADES of software development experience. If you haven't used it to solve your problem, I must conclude that either a) you are not trying, or b) you are not understanding the actual steps.

    Sure... in that one you are looking at. Did you inadvertently put this script in the scene more than once? It happens a LOT. That's why I called it out in the nullref post.

    Good luck!
     
  11. unity_B3D5697DFA04615A0786

    unity_B3D5697DFA04615A0786

    Joined:
    Jun 11, 2021
    Posts:
    25
    what code do you want me to show you, tell me and I'll show you, I only have 2 elements "PLAYER" WHICH CONTAINS THE ELEMENTS OF THE PHOTO (RIGIDBODY, CHARACTERCONTROLLER, ETC) AND WITHIN "MARIO" WHICH HAS THE ANIMATOR WITH THE ANIMATIONS ALREADY READY TO USE.
    IN "PLAYER" WHERE IS THE C# OF THE MOVEMENT I PUT "PUBLIC ANIMATOR ..." AND IN THE UNITY INPECTOR DRAG THE "MARIO" CONTROLLER
    What am I doing wrong, what do I need to show so that they understand the error. (I know there is something wrong, you don't need to send me a link of a generic error, like the other user did)
     
  12. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    Show us the screenshot of your inspector where it shows the public field and what you have dragged into it. Based on the error it says that you dragged the Player object into it, but you are saying that you dragged the Mario object. Please show us as otherwise we are just guessing.
     
  13. unity_B3D5697DFA04615A0786

    unity_B3D5697DFA04615A0786

    Joined:
    Jun 11, 2021
    Posts:
    25
    upload_2022-9-26_21-42-26.png
    upload_2022-9-26_21-42-30.png

    upload_2022-9-26_21-42-33.png
    upload_2022-9-26_21-43-1.png
     
  14. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    PraetorBlue likes this.
  15. unity_B3D5697DFA04615A0786

    unity_B3D5697DFA04615A0786

    Joined:
    Jun 11, 2021
    Posts:
    25
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player_Mov : MonoBehaviour
    6. {
    7.     CharacterController characterController;
    8.  
    9.     public float moveSpeed;
    10.     public float walkSpeed = 4;
    11.     public float sprintSpeed = 6;
    12.     public float jumpSpeed = 8.0f;
    13.     public float jumpTime = 0.35f;
    14.     public float jumpTimeCounter;
    15.     public ParticleSystem dust;
    16.  
    17.     private bool IsJumping;
    18.     public Animator Animator;
    19.  
    20.  
    21.     public float gravity = 20.0f;
    22.  
    23.  
    24.     private Vector3 moveDirection = Vector3.zero;
    25.     private float Horizontal;
    26.  
    27.  
    28.     void Start()
    29.     {
    30.         characterController = GetComponent<CharacterController>();
    31.       Animator = GetComponent<Animator>();
    32.  
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         if (characterController.isGrounded)
    38.         {
    39.             // We are grounded, so recalculate
    40.             // move direction directly from axes
    41.             Horizontal = Input.GetAxisRaw("Horizontal");
    42.  
    43.             if(Horizontal < 0.0f) transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f);
    44.             else if (Horizontal > 0.0f) transform.localScale = new Vector3(1.0f,1.0f,1.0f);
    45.  
    46.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, 0.0f);
    47.  
    48.             if(Input.GetKey(KeyCode.LeftShift))
    49.             {
    50.                 moveSpeed = sprintSpeed;
    51.             }
    52.             else
    53.             {
    54.                 moveSpeed = walkSpeed;
    55.                 Animator.SetBool("walking", Horizontal != 0.0f);
    56.  
    57.             }
    58.  
    59.             moveDirection *= moveSpeed;
    60.  
    61.             if (Input.GetButtonDown("Jump"))
    62.             {
    63.                 IsJumping = true;
    64.                 jumpTimeCounter = jumpTime;
    65.                 moveDirection.y = jumpSpeed;
    66.                 CreateDust();
    67.             }
    68.  
    69.         }
    70.         if (Input.GetButton("Jump") && IsJumping == true)
    71.         {
    72.             if(jumpTimeCounter > 0)
    73.             {
    74.                 moveDirection.y = jumpSpeed;
    75.                 jumpTimeCounter -= Time.deltaTime;
    76.                 CreateDust();
    77.             }
    78.             else
    79.             {
    80.                 IsJumping = false;
    81.             }
    82.         }
    83.         /*if(Input.GetButtonUp("Jump"))
    84.         {
    85.                 IsJumping = false;
    86.         }*/
    87.         // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
    88.         // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
    89.         // as an acceleration (ms^-2)
    90.         moveDirection.y -= gravity * Time.deltaTime;
    91.  
    92.         // Move the controller
    93.         characterController.Move(moveDirection * Time.deltaTime);
    94.     }
    95.     void CreateDust()
    96.     {
    97.         dust.Play();
    98.     }
    99. }
     
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Line 31 is wiping out anything you dragged in. Why is line 31 present?

    If you had ACTUALLY DONE the 3-step process I listed above, you would have been done half a day ago.

    This is just another example of defiantly resisting the three step process and it will just wait for you until you give in, Don't worry, the record so far is holding out for just over one week. It is the ONLY thing that will fix a NullReferenceException. THE ONLY THING!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null
    - Identify why it is null
    - Fix that
     
  17. unity_B3D5697DFA04615A0786

    unity_B3D5697DFA04615A0786

    Joined:
    Jun 11, 2021
    Posts:
    25
    I accept my mistake, it was a comment of frustration, I am practicing during my free time.
    Now after seeing line 31, and seeing that guide, I tried removing it, and now it works, but my question now is, that Animator should not go in that case in the "Start" method?
     
  18. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    No, straight
    GetComponent<T>
    looks on the same object as the component that's doing the query. So it was looking on the Player game object, not Mario.

    If you have a reference to another object, or any of its components, you can GetComponent that object for any of its components.

    As it was in your example, it was looking on the same object, finding nothing, and returning null.