D
D
Daniel23102021-06-28 11:38:10
Unity
Daniel2310, 2021-06-28 11:38:10

NullReferenceException: Object reference not set to an instance of an object vubros.onDown - Bug in Unity?

Good afternoon, I got this error in Unity: NullReferenceException: Object reference not set to an instance of an object vubros.onDown.
Please help me to understand why?
code 1 :

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

public class vubros : MonoBehaviour
{
   
    public GameObject item;
    private Transform player;
    public static bool butslives;
    public static float timerperemenai = 0;
  
    public static bool spedbuts;
    public GameObject timer;
    //public static float resettime = 0;
   // public static float resettimechet = 0;
   // public static bool reset = false;


    public static bool timeropisanie = false;
    public static bool speedbutsp = false;


    private void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        butslives = false;
        



       ;
    }
    public void spawnDropedItem()
    {
        Vector3 playerPos = new Vector3(player.position.x + 3, player.position.y + 1,9);
        Instantiate(item, playerPos, Quaternion.identity);
    }
    public void Butslives()
    {
        
        
            if (plaeyr.health < 3) 
            {
                plaeyr.health += 1;
                Debug.Log(plaeyr.health + " plaeyr.health");
                butslives = true;
                Debug.Log("жизнь +");
                Destroy(gameObject);
            }
            else
            {
                Destroy(gameObject);
                Debug.Log("-");
            }
        
    }
public void speedbuts()
    {
        plaeyr.speedbuts += 2.5f;
        timerperemenai++;
        speedbutsp = false;
        //spedbuts = true;
        //porehoddliof.oftimer = true;
        //resettime++;
        //resettimechet++;

        
        
        
        
    }
    
    private void Update()
    {
        if (resettimechet >= 2)
            reset = true;
            resettimechet--;
           
    }
    public void onDown()
    {
        //opisanie.presed = true;
        speedbutsp = true;
        GetComponent<opisanie>().opisaniegameob.SetActive(false);//ругается на эту строчку

    }
    public void onUp()
    {
        //opisanie.presed = false;
        StartCoroutine(timer());

    }
    private IEnumerator timer()
    {
        yield return new WaitForSeconds(2.0f);
        timeropisanie = true;
    }



}

code 2 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class opisanie : vubros
{
    //public static bool presed = false;


    public Text text;
    public  GameObject opisaniegameob;
    public static bool timeriopisanie; 
    //private bool zadershka = true;
    
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (vubros.speedbutsp == true && vubros.timeropisanie == false)
        {
            speedbuts();
        }
        else if (vubros.speedbutsp == false && vubros.timeropisanie == true)
        {
            opisaniegameob.SetActive(true);
        }
    }
}

Please do not say that you should have read the Internet first, read the documentation. I read, I did not understand what I have wrong. (sorry I'm stupid (: )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrSlowpoke, 2021-06-28
@MrSlowpoke

Does the error occur immediately or after the second execution of the function?
Most likely the problem is that you disable the component, and then try to find it.
As an option, store a link to the component in advance, then there will be no error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question