how to add to a variable if theres is added to another variable? c#

Hey There guys :slight_smile:
I am trying to do so, that if curMagic increases with just 1, then maxMana gets increased by 15.
This is my code :

		if (GetComponent<PlayerStat>().curMagic >= GetComponent<PlayerStat>().curMagic){
           	maxMana += 15;

But it just keeps increasing maxMana all the time :frowning:
(Dont mistaken me, i am new to Programming and i am struggling through all of this doing my best :slight_smile: )

Not really a clear question, is it in the update function. if so then it is probably why… It’s best to be detailed with your questions and maybe show more than just 2 lines. How about this, go to youtube and give bergzergarcade a quick look. His vids might help you figure something out.

Well, the Answer i am looking for was what to do with this line “if (GetComponent().curMagic >= GetComponent().curMagic){”
Because, i need to do so that when curMagic increases by one, maxMana is also increased by 15.
It is not in the update function , it is in “public void AddjustCurrentMana(int adj) {”

About burgzergarcade, i have seen alot of the videos and they are really helpful, mostly it is him i learned the basic i can do from.

What’s the condition that makes the maxMana increase? Is this when the player levels up?

Yeah :slight_smile:

Yeah, figured. Okay, and I’m assuming that the AddjustCurrentMana is called FROM Update. And that is why it constantly increases. But you’re going to need to make a condition that calls the increase of the stats once. Off the top of my head, you could do something like if the current exp reaches the max exp to level up then up the stats and put the current exp back to zero.

Dr3w, it is not placed in the Update :slight_smile:
PlayerMana.cs

using UnityEngine;
using System.Collections;

public class PlayerMana : MonoBehaviour {
	public int maxMana = 100;
	public int curMana = 100;
	
	public float ManaBarLength;


	void Start () {
	ManaBarLength = Screen.width / 2;
	}
	

	void Update () {
	AddjustCurrentMana(0);
		
	}
	
   
	public void AddjustCurrentMana(int adj) {
	  curMana += adj;	
		
		if(curMana < 0)
			curMana = 0;
		
		if(curMana > maxMana)
			curMana = maxMana;
		
		if(maxMana < 1)
			maxMana = 1;
		
		ManaBarLength = (Screen.width / 2) * (curMana / (float)maxMana);
		
		if (GetComponent<PlayerStat>().curMagic >= GetComponent<PlayerStat>().curMagic){
           	maxMana += 15; 

        	}
	}
}

And i already got something that sets the exp to 0
PlayerExp.cs

using UnityEngine;

using System.Collections;

 

public class PlayerExp : MonoBehaviour {

    public int curExp = 0;

    public int maxExp = 150;
	

    void Start () {

    }

    

 

    void Update () {

    AddjustCurrentExp(0);

        

    }
        public void AddjustCurrentExp(int adj) {

      curExp += adj;
        

        

        

        if(curExp >= maxExp)

        {

           curExp = curExp - maxExp;

           	maxExp =(int)(maxExp * 1.3f);

           	GetComponent<PlayerLvl>().curLvl += 1;   
			GetComponent<PlayerMana>
			
		//	playerstat.curStr += 15;
			
		//	playerstat.curVit += 15;
			
		//	playerstat.curMagic += 15;
		}
		
        if (Input.GetKey(KeyCode.Z)){ 

        curExp += 100;   

        }
	
	
    }   

}

Well, it is being CALLED from Update. When you put the other method in Update its still being called every frame. But, yeah you also don’t have the curExp going back to zero. Uhm, I whipped up a real quick test but I personally don’t want to implement it into what you’re doing. haha Sorry but I’m slacking on my own stuff as it is. haha But here ya go.

public int maxExp = 3,
		curExp = 0,
		stat = 0;

// Update is called once per frame
	void Update ()
	{
		if (curExp >= maxExp)
		{
			stat += 3;
			curExp = 0;
		}
		
		if (Input.GetKeyDown(KeyCode.Z))
		{
			curExp++;
		}
	}

This Script dident really work :frowning:
I was looking for something to just simplify it to something that looked like this but worked!
"
(GetComponent().curMagic += 1){
maxMana += 15;

}
"

Bump anyone?..

The issue is with your logic statement.

        if (GetComponent<PlayerStat>().curMagic >= GetComponent<PlayerStat>().curMagic){

            maxMana += 15; 

 

            }

Will Always equal true because you are comparing the same two variable values.

Observe;
GetComponent().curMagic = 1;
if (GetComponent().curMagic >= GetComponent().curMagic){ //If statement translation to pseudocode;

IF 1 is MORE THEN, or EQUAL to 1 THEN.

Your logic will always return as true, basically meaning that your maxMana variable will automatically increase by 15 every iteration.

I know my code is kinda a paradox, it was the last thing i tried, before looking for help on the forum :slight_smile:

Bump?? :frowning:

The problem is as sarmth said, you are checking the variable against itself for equality, which will always return true.

The easy solution is to create a new variable, for example oldMagic

When your guy levels up, set this variable equal to curMagic

This is pseudocode, don’t expect it to work on your own project. Just there to give you an idea of how to do it

void LevelUp()
{
  oldMagic = curMagic; 
  curMagic = curMagic + 1;  // Could also do curMagic++ your choice

}

void increaseMana()
{
  if(curMagic == oldMagic + 1) // this is your logic for increasing magic
  {
    maxMana += 15; 
  }
}

well, this makes maxMana increase all the time :confused:

Changed it to ++ instead of +1, now it works like a charm! :smile:
Thank you man :wink: