L
L
logxy2020-12-08 19:40:19
C++ / C#
logxy, 2020-12-08 19:40:19

unity 2d error, what to do?

I wrote another script for playing c# in unity(2d) and I already had a written script to control the hero, but after adding the script I get an error (NullReferenceException: Object reference not set to an instance of an object
Slot.Update () ( at Assets/Scripts/Inventory/Slot.cs:14)
and the character is not walking yet,
here is the character control code:

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

public class PlayerControll : MonoBehaviour
{
    public float speed = 1500f;
    private Rigidbody2D rb;
    private bool faceRight = true;
    void Start()
    {
        rb = GetComponent <Rigidbody2D> ();
       
    }
    void Update()
    {
        float moveX = Input.GetAxis ("Horizontal");
        rb.MovePosition (rb.position + Vector2.right * moveX * speed * Time.deltaTime );

        if(Input.GetKeyDown (KeyCode.Space)){
            rb.AddForce(Vector2.up * 2000);
        }
        if(moveX > 0 && !faceRight)
        {
            flip();
        }else if (moveX < 0 && faceRight){
            flip();
        }
        if(Input.GetKeyDown (KeyCode.LeftShift))
        {
            speed = 200f;
        }
        if(Input.GetKeyUp (KeyCode.LeftShift)){
            speedest();
        }
       
    }
    void flip(){
        faceRight = !faceRight;
        transform.localScale  = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
    }
    void speedest(){
        speed = 20f;
    }
}

All links included

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2020-12-08
@logxy

Well, here are the following options:
- the Inventory component was not found on the object with the Player tag
- The isFull inventory is not initialized.
Learn to debug. At least the logs. You have something Null and it is clearly written about it - it remains to understand which of the two

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question