@Daniil2411

Ошибка в Unity — NullReferenceException: Object reference not set to an instance of an object vubros.Update ()?

Добрый день, у меня есть во такой код:

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;
private Image sheetimg;
public float cooldawn;
private bool spedbuts;

private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
butslives = false;
sheetimg = GetComponent();
}
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);
}
}
public void speedbuts()
{
plaeyr.speedbuts += 2.5f;
spedbuts = true;


}
private void Resettimespeedbuts()
{
sheetimg.fillAmount = 1;
}
private void Update()
{
if (spedbuts)
{
sheetimg.fillAmount -= 1 / cooldawn * Time.deltaTime;//ругается на эту строчку, cooldawn = 5

if (sheetimg.fillAmount <= 0)
{
sheetimg.fillAmount = 1;
plaeyr.speedbuts -= 2.5f;
gameObject.SetActive(false);
spedbuts = false;
}
}
}

}
  • Вопрос задан
  • 49 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Hixac
У вас неправильное присвоение переменной. GetComponent() сам ничего не даст, если не конкретизировать тип данных, который вам нужен.

О ошибке, она означает, что в строке используется переменная (ссылка) с нулевыми данными, а.к.а Null. Такое бывает, если ничего не присвоить переменной например.

sheetimg = GetComponent(); //Неправильно!

sheetimg = GetComponent<[Тип, который вам нужен]>(); //Правильно.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы