Hi everyone, I defined a class named “LvlManager” as following:
public class LvlManager : MonoBehaviour
{
public static LvlManager Instance { get; private set; }
[Header("References")]
public static int level = 0 ;
public Transform startPos;
public Transform[] path;
public int path_idx = 0;
[SerializeField] public int killingsToGet; // minimum number of killed enemy to win !!
[SerializeField] public static int totalEnergy;
[SerializeField] public static GameOver gameOver;
[SerializeField] public WINNING_bg wining;
public bool gameRunning;
public static int money;
public static int killedEnemies;
private void Awake()
{
Debug.Log("LevelManager Awake");
gameRunning = true;
///setting variabla instance
if (Instance != null && Instance !=this)
{
Destroy(gameObject);
return;
}
Instance = this;
money = 8; // starting valie
//Instance.gameRunning = true; not starting value
totalEnergy = 3;
killedEnemies = 0;
DontDestroyOnLoad(gameObject);
}
private void Update()
{
if (killedEnemies >= killingsToGet) // WON condition killingsToGet
{
if (wining != null)
{
Win_Window();
}
}
}
public void LoadNextLevel() {
int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
if (nextSceneIndex < SceneManager.sceneCountInBuildSettings)
{
SceneManager.LoadScene(nextSceneIndex);
} else
{ Debug.LogWarning("Sei già all'ultimo livello! ");
}
}
//
public static void Game_Over()
{
gameOver.setting();
}
public void Win_Window()
{
if (wining != null)
{
wining.winning();
}
else
{
return;
}
}
}
when I try calling it in another class (by using the Visual Studio Code IDE), It looks like the class LvlManager doesn’t exist…in the Vs Code extensions I already installed C# utilities…but It doesn’t work…
the other class from where I try calling the former class is:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using TMPro;
public class Menu_TD : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
[Header("tefers")]
[SerializeField] TextMeshProUGUI currencyMoneyUI;
private void OnGUI()
{
currencyMoneyUI.text = LvlManager.//LvlManager.Instance.money.ToString();
}
public void SetSelectedTower(int _selectedTower)
{
//indexSelectedTower = _selectedTower;
}
}
ok I checked both packages in UNITY and extensions in Vs Code; they are all installed; despite it I am still getting error which indicates me following error message: Member ‘LvlManager.money’ cannot be accessed with an instance reference; qualify it with a type name instead
considering I defined the following class:
using UnityEngine;
using UnityEngine.SceneManagement;
public class LvlManager : MonoBehaviour
{
public static LvlManager Instance { get; private set; }
[Header("References")]
public static int level = 0 ;
public Transform startPos;
public Transform[] path;
public int path_idx = 0;
[SerializeField] public int killingsToGet; // minimum number of killed enemy to win !!
[SerializeField] public static int totalEnergy;
[SerializeField] public static GameOver gameOver;
[SerializeField] public WINNING_bg wining;
public bool gameRunning;
public static int money;
public static int killedEnemies;
private void Awake()
{
Debug.Log("LevelManager Awake");
gameRunning = true;
if (Instance != null && Instance !=this)
{
Destroy(gameObject);
return;
}
Instance = this;
money = 8; // starting valie
totalEnergy = 3;
killedEnemies = 0;
DontDestroyOnLoad(gameObject);
}
private void Update()
{
if (killedEnemies >= killingsToGet) // WON condition killingsToGet
{
if (wining != null)
{
Win_Window();
}
}
}
public void LoadNextLevel() {
int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
if (nextSceneIndex < SceneManager.sceneCountInBuildSettings)
{
SceneManager.LoadScene(nextSceneIndex);
} else
{ Debug.LogWarning("Sei già all'ultimo livello! ");
}
}
public static void Game_Over()
{
gameOver.setting();
}
public void Win_Window()
{
if (wining != null)
{
wining.winning();
}
else
{
return;
}
}
}
I am stuck on this… @Kurt-Dekker , firstly I try to solve this problem, after that I will be going to using SingletonSimple approach. Thanks.
You have marked money as static. REMOVE the static… same goes for killedEnemies:
That may cause additional errors which you must fix according to how you want to engineer things.
NOTE: there is nothing I can type in this box to cause you to understand the difference between static and instance variables, but I urge you to turn your brain to this until you grasp it. There’s plenty of tutorials and/or blog entries describing the differences.
Unity is moving towards FEPM. This requires manually cleaning (tagging) static fields that must be reset when entering playmode or they’ll keep their previous state.
It’s global state. That’s going to lock you into strict single-use (you can’t have two “LevelMngr”) and they become accessible anywhere, which entangles the code. Changing existing code to work in “multiples” and thus being forced to remove static fields and debugging global state bugs are some of the nastiest things a developer will encounter.
There’s always a way to make something work without static, and it’s often just one additional level of indirection.
The real issue here is that you’re approaching learning programming the wrong way. Instead of learning the fundamentals and building a solid foundation, you keep focusing only on solving whatever problem appears in front of you at the moment. As a result, you continue making the same basic mistakes over and over again. Combined with the fact that you often ignore about 90% of the advice you’re given and only focus on the 10% you believe is immediately relevant, this makes it very difficult to actually learn how to program. At best, it provides a temporary fix for the problem of the day.
One example is your reply here:
If you’re already aware of a solution to an issue, you should implement it. There’s little value in postponing it until you’ve solved some other problem first.
Another example is in this thread:
You clicked “Like” on the answer, but that doesn’t mean much if you never apply the advice. Not providing feedback about whether a solution worked, ignoring answers, and immediately moving on to the next problem only leads to going in circles and repeating the same mistakes.
The advice Kurt-Dekker gave you in this thread had already been given to you before:
Yet it was ignored, and the result is that you’re facing the same issue again.
The same applies to the advice CodeSmile gave in this thread:
This is also advice they had already given you previously:
All of this is relevant because if you had actually followed that advice, you wouldn’t be facing the issue you’re dealing with today. The error:
is telling you that LvlManager.money cannot be accessed through an instance reference (using the singleton instance) because it is declared as a static field:
public static int money;
If, instead of focusing only on the immediate problem, you took the time to read and understand the advice being given, this wouldn’t be an issue. In fact, in earlier versions of your script, money was already implemented as an instance member, for example here:
There’s no benefit in ignoring parts of an answer because you think they aren’t relevant. There’s no benefit in moving on to the next problem without reporting whether a solution worked. There’s also no benefit in trying random keywords until something appears to work. That approach simply doesn’t lead to meaningful progress.
Take some time to learn the fundamentals instead of treating every compiler error as an isolated problem that requires a new forum post. Otherwise, you’ll continue fixing one error by applying only part of the advice, creating another issue, asking for help again, and repeating the same cycle.
This LevelManager script has been causing problems for over a month. Realistically, if you had combined and applied the advice you’ve already received across your various topics, it could have been resolved within a week at most.
This also ties directly into the topic you created here:
The best thing you can do before even thinking about building a portfolio is to learn the fundamentals. Constantly seeking answers for individual problems, while ignoring most of the explanations behind those answers, won’t help you develop the skills needed to solve problems independently. Likewise, ignoring feedback and repeatedly creating new topics about the same underlying concepts only ensures that the same issues will continue to resurface.
Yes, it is perfectly right..The problem was I wanted to run too much but in the end I was just wasting time…unfortunately I have some problems which will block me during the summer period…By the way, I will concentrate first on studying and learning fundamentals…