D
D
Daniel24112021-06-25 16:38:33
C++ / C#
Daniel2411, 2021-06-25 16:38:33

Error in Unity - NullReferenceException: Object reference not set to an instance of an object vubros.Update()?

Good afternoon, I have in such code:

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

public class vubros : MonoBehavior
{

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("life +");
Destroy(gameObject);
}
else
{
Destroy(gameObject);
}
}
public void speedbuts()
{
plaeyr. speedbuts += 2.5f;
spedbutts=true;


}
private void Resettimespeedbuts()
{
sheetimg.fillAmount = 1;
}
private void Update()
{
if (spedbuts)
{
sheetimg.fillAmount -= 1 / cooldawn * Time.deltaTime;//swears at this line, cooldawn = 5

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

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hixac, 2021-06-25
@Hixac

You have the wrong variable assignment. GetComponent() won't do anything by itself unless you specify the data type you want.
About the error, it means that the string uses a variable (reference) with null data, a.k.a Null. This happens if you do not assign anything to a variable, for example.

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

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question