Answer the question
In order to leave comments, you need to log in
Unity NullReferenceException: Object reference not set to an instance of an object how to overcome?
Good day, I decided to start working with Unity over the weekend. Knowledge of C# is relatively low. To begin with, I decided to make a 2D platformer, it seems that this is the easiest.
the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player_controls : MonoBehaviour
{
Rigidbody2D rb;
Animator anim;
public float speed = 6f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
jump();
}
if (Input.GetAxis("Horizontal") == 0)
{
anim.SetInteger("animation", 1);
}
else
{
FlipHero();
anim.SetInteger("animation", 2);
}
}
void FlipHero()
{
if (Input.GetAxis("Horizontal") < 0)
{
transform.localRotation = Quaternion.Euler(0, 180, 0);
} else
{
transform.localRotation = Quaternion.Euler(0, 0, 0);
}
}
void FixedUpdate()
{
rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
}
void jump()
{
rb.AddForce(transform.up * 12f, ForceMode2D.Impulse);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question