Good day!
I have a problem. I’m at Unity recently and can’t understand what kind of error?
Do you have to pay attention to it or can you just skip it?
Here is the error:
Field ‘BuyCars.Money’ is never assigned to, and will always have its default value 0
And here is the code itself:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BuyCars : MonoBehaviour
{
private int car1;
private int Money;
public void cars1()
{
if(Money >= 20000)
{
Debug.Log(“Вы успешно преобрели Ваз 2101.”);
}
else
{
Debug.Log(“У Вас не хватает денег!”);
}
}
}
Пожалуйста, помогите мне решить, просто не пишите, что вы можете найти в Google и так далее.
Спасибо всем заранее ...
Hello,
In your code, you never set a value to Money , therefore, the compiler is warning you that you probably should assign a value to it.
If you assign a starting value to Money , the warning will go away, like this
private int Money = 10;
Ted_Wikman:
Hello,
In your code, you never set a value to Money , therefore, the compiler is warning you that you probably should assign a value to it.
If you assign a starting value to Money , the warning will go away, like this
private int Money = 10;
Hello!
Look, my Money value will change. How can I make it for example not equal only 10?
justLC
January 16, 2020, 6:41pm
4
You could make it a public variable instead of private and change it in the Inspector.
Edit: Alternatively you can use the [SerializeField] tag
Ted_Wikman:
Hello,
In your code, you never set a value to Money , therefore, the compiler is warning you that you probably should assign a value to it.
If you assign a starting value to Money , the warning will go away, like this
private int Money = 10;
And another question. If I want to copy a variable such as Money from, for example, the cs file of the Game into the cs file of BuyCars. How to do it? Thanks in advance!
Thank you very much, you helped me a lot. Have a nice day or evening.
Ted_Wikman:
Hello,
In your code, you never set a value to Money , therefore, the compiler is warning you that you probably should assign a value to it.
If you assign a starting value to Money , the warning will go away, like this
private int Money = 10;
Thank you very much, you helped me a lot. Have a nice day or evening.