C
C#4w ago
TheTar

Can someone Help me understand why my Mana code is not working?

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class PlayerStats : MonoBehaviour public Slider pManaBar; public float pMana; public float pMaxMana = 100; void RegenerateMana() { IceBall ice = gameObject.GetComponent<IceBall>(); if (ice != null) { if (pMana < pMaxMana & !ice.usingMana) { pMana += pManaRegen * Time.deltaTime; if (pMana > pMaxMana) pMana = pMaxMana; UpdateUI(); Debug.Log("UsingMANA"); } } } public void UseMana(float amount) { pMana -= amount; UpdateUI(); if (pMana < 0) pMana = 0; Debug.Log("UseMana"); } using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; public class IceBall : MonoBehaviour { public float manaIce = 50; public bool usingMana = false; private void OnTriggerEnter2D(Collider2D collision) { PlayerStats stats = collision.gameObject.GetComponent<PlayerStats>(); if (stats != null)
{ Debug.Log("What stats", null); stats.UseMana(manaIce * Time.deltaTime); usingMana = true; } else { usingMana = false; } } }
11 Replies
Angius
Angius4w ago
Define "not working" From the get-go, I see you used & instead of && there. The former is a bitwise and, while the latter is the logical and that you probably want to use
TheTar
TheTarOP4w ago
So the Iceball script seems like it is not running, So the UseMana() in PlayerStats is not being used. I tried debug.log but nothing ever comes up, so I don't think the logic is right but I used the same logic for the arrrows script and it works fine
Angius
Angius4w ago
Ideally, you would use the debugger instead of Debug.Log() everywhere That way, you would be able to step through the code, see how it executes and when
TheTar
TheTarOP4w ago
Intresting I didn't know that was a thing Let me see how to do that
Buddy
Buddy4w ago
$debug
MODiX
MODiX4w ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Buddy
Buddy4w ago
Note that you must press 'Attach to Unity' if you use Visual Studio or Rider
TheTar
TheTarOP4w ago
oh thx so much
TheTar
TheTarOP4w ago
I think it's my logic in the code that doesn't let it run, Bec nothing comes up until I put if (stats == null), But I don't need it to equal null.
No description
arion
arion3w ago
It means your stats object is null. Also you could try using this attribute to help https://docs.unity3d.com/6000.0/Documentation/ScriptReference/RequireComponent.html you don't need gameobject.GetComponent<T> since GetComponent<T> does the same thing the monobehaviour is attached to the respective game object they also show this in the example use of the attribute Is there a reason for the p prefix for some of your fields in the PlayerStats monobehaviour? pManaBar and pMana Looking at the 2 monobehaviours, its kinda wonky, both components relying on each-other being present. A circular dependency
TheTar
TheTarOP3w ago
You might be right, ill look into it when I have more time. Thanks for the info, I really appreciate it. For the p before some of the stats its bec playerManaBar or playerMana, was thinking of making enemy's have a mana bar and such later, so that is for me not to get confused. Thanks again for the help! I need to write this game doc before the night ends for my professor now, any more feedback and I will look into it tm when I have more time. Sleep well guys!!
Want results from more Discord servers?
Add your server