-2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player: MonoBehaviour
{
    [SerializeField]
    private float moveForce = 1f;
    [SerializeField]
    private float jumpForce = 11f;
    private float movementx;
    private Rigidbody2D myBody;
    private SpriteRenderer sr;
    private Animator anim;
    private string WALK_ANIMATION = "walk";
    // Start is called before the first frame update
   
    private void awake()
    {
        myBody = GetComponent<Rigidbody2D>();
        sr = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();

    }
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        PlayerMoveKeyboard();
        AnimatePlayer();  // error is coming in this line
//when i am trying call this function above it is showing error which i have written down
    }
    void PlayerMoveKeyboard ()
    {
        movementx = Input.GetAxisRaw("Horizontal");
        transform.position += new Vector3(movementx, 0f, 0f) * moveForce*Time.deltaTime;
      
    }
    void AnimatePlayer()
    {
        if(movementx>0)
        {
            anim.SetBool(WALK_ANIMATION, true);
        }
        else if(movementx<0)
        {
            anim.SetBool(WALK_ANIMATION, true);

        }
        else
        {
            anim.SetBool(WALK_ANIMATION, false); // error is coming in this line

        }
    }
}

The error is coming this in unity

NullReferenceException: Object reference not set to an instance of an object Player.AnimatePlayer () (at Assets/scripts/Player.cs:53) Player.Update () (at Assets/scripts/Player.cs:34)*/

derHugo
  • 83,094
  • 9
  • 75
  • 115
Goki 27
  • 1
  • 1
  • 5
  • 2
    Well, is your `awake` method being called? If it is, does `GetComponent` return a non-null value? Note that you shouldn't just explain what you *expect* to happen, but what you have *proved* is happening via the debugger or logging. – Jon Skeet Jul 23 '21 at 13:38
  • I agree with Jon Skeet. Try logging the value of `anim` on Update to see what it returns. If it returns `null`, then for some reason, `anim` isn't being set to your `GetComponent<>()`. This may happen because of two reasons: `Awake()` isn't being called, or because you may not have an Animator component attached to the GameObject. – undevable Jul 23 '21 at 14:27
  • @JonSkeet sir i called awake but its not working and GetComp is already there.. its not working (thankyou so much for helping) – Goki 27 Jul 23 '21 at 16:24
  • @undevable sir actually I am just new to this and what you said at the last animator maybe not attached I didn't understand but I have attached the animator controller with that ..... (thankyou so much for helping) – Goki 27 Jul 23 '21 at 16:24
  • *Where* have you called `awake()`? There's no sign of that in your code. "It's not working" really doesn't tell us much. – Jon Skeet Jul 23 '21 at 16:26
  • @JonSkeet after seeing your reply i did it practically right now – Goki 27 Jul 23 '21 at 16:27
  • Sorry, I don't understand what you mean at all. You need to call `awake()` from elsewhere in your code, e.g. in `Start`. It's not something you decide to do manually. – Jon Skeet Jul 23 '21 at 16:29
  • ok sir thankyou so much – Goki 27 Jul 23 '21 at 16:31
  • Please use the correct tags! Note that [`unityscript`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a JavaScript flavor like custom language used in early Unity versions and is long **deprecated** by now! If you don't use that language don't use that tag. Also just because you use a certain IDE (`visual-studio`) doesn't automatically mean that this question is about that IDE – derHugo Sep 07 '21 at 18:38

1 Answers1

-1

Capitalize the Awake function:

private void Awake()
{
    myBody = GetComponent<Rigidbody2D>();
    sr = GetComponent<SpriteRenderer>();
    anim = GetComponent<Animator>();
}
MoreFoam
  • 624
  • 1
  • 6
  • 25
  • done sir that was a mistake but it didn't help – Goki 27 Jul 23 '21 at 16:35
  • In the Unity editor, click on your Player object and share a screenshot of the Inspector on your original post. The only thing that could be null on the line you mentioned is the object named "anim": "anim.SetBool(WALK_ANIMATION, false);". We need to make sure that your Player object has an Animator component attached and that the Controller property on the Animator component is populated. – MoreFoam Jul 23 '21 at 16:46
  • Please add some context to explain the code sections (or check that you have not incorrectly formatted all of your question as code). stackoverflow keep saying this after writing so much and I am new so I cant post picture as the link was only available so I am posting link here please help sir https://easyupload.io/alutyo – Goki 27 Jul 23 '21 at 16:59
  • That looks fine, I have actually done that same tutorial. Hmm.. I'm not quite sure what else could be wrong. Are you sure you saved your script after capitalizing the Awake function? If that is not the issue, I suggest rewatching the lecture that you encountered the error on. For each step that the instructor takes, make sure yours looks the same. – MoreFoam Jul 23 '21 at 17:15
  • Awesome! Good luck with the rest of the course. Feel free to accept this answer or upvote if it helped you. Someone else downvoted the answer, but I'm not sure why. I suspect it was someone who knows C#, but is not familiar with Unity, so they have no idea why my answer is correct. – MoreFoam Jul 23 '21 at 17:31
  • actually idk how to vote and as my acc is new so I am not allowed to but I did check on this answer helped me but I don't know what happened .. it worked – Goki 27 Jul 23 '21 at 17:54
  • I think you just needed to save the script and reload your script assemblies. Unity reloads script assemblies automatically after saving changes. I can duplicate the issue you see by changing my Awake function name to "awake". That is why I am confident that was the issue. – MoreFoam Jul 23 '21 at 17:56
  • ok sir but when i did that the and saved and closed unity and opened again so everything working but one thing happened was the player animation was gone .. one error was gone one came so I programming again – Goki 27 Jul 23 '21 at 18:03
  • sir do you have any youtube channel or anything like that can you give me your details so that I can connect with you – Goki 27 Jul 23 '21 at 18:07
  • I do not, I'm just a normal person trying to learn more about Unity and help others do the same. Are you still running into an issue? – MoreFoam Jul 23 '21 at 18:09
  • actually i am not getting the issue but I cant get the animation now the player moves but no animation even tho I called it – Goki 27 Jul 23 '21 at 18:27
  • You should review your Animator Controller. Double click on the controller to bring up the Animator window. You should have an Idle state and a Walk state. Each of those states should have it's Motion property set to it's animation (e.g. Walk state should have its Motion property set to use the Walk animation clip). Then there should be a transition from Idle to Walk and Walk to Idle. Each transition should look at the Walk boolean. If true, it should transition to the Walk animation. If false, it should transition to idle. For this issue, it may be best to rewatch the video and compare. – MoreFoam Jul 23 '21 at 18:34
  • sir if you have time can you check my code and unity please because I don't know why I keep getting the same error if I use the code ..... https://discord.gg/CSG5X2Fs this is my discord link can you please help me out its 2am right now I will sleep bye – Goki 27 Jul 23 '21 at 20:33