C# heeelp please!

Okay, so i am currently working on making a script for leveling up, I am not super new to c#, but i am definitly not pro.
I want it to everytime a Player levels up, the maxExp to be multiplied by 2, and curExp to be reset to 0.
it dont really want to work :confused:
Here is my Playerlvl.cs

using UnityEngine;
using System.Collections;

public class PlayerLvl : MonoBehaviour {
	public int maxLvl = 75;
	public int curLvl = 01;	
	public float LevelLength;
	public int curExp = 0;
	public int maxExp = 100;

	void Start () {
	LevelLength = Screen.width / 8;
	}
	

	void Update () {
	AddjustCurrentLvl(0);	
	AddjustCurrentExp(0);
		
	}
	
	
	void OnGUI(){
	GUI.Box(new Rect(10, 260, LevelLength, 20), curLvl + "/" + curExp);	
	}
	
	
   
	public void AddjustCurrentLvl(int adj) {
	  curLvl += adj;	
		
		
	
		
		if(curLvl > maxLvl)
			curLvl = maxLvl;
		
		if(maxLvl < 1)
			maxLvl = 1;
	}
	
	
		public void AddjustCurrentExp(int adj) {
	  curExp += adj;
		
		
		
		if(curExp == maxExp)
	   	maxExp *= 2;
                curLvl += 1;
		curExp = 0;
			
		if (Input.GetKey(KeyCode.Z)){ 
		curExp += 10;	
		}
	}   
}

Well, adj is always 0, so your exp wont increase unless you press Z,

As for the rest of your code you’re missing brackets:

if(curExp == maxExp)
      maxExp *= 2;
            curLvl += 1;
      curExp = 0;

should be

if(curExp >= maxExp)
{
   maxExp *= 2;
   curLvl += 1;
   curExp = 0;
}

That starts on line 48.

EDIT: I’d also change the == to >= so:

if(curExp >= maxExp)
{
   curExp = curExp - maxExp;
   maxExp *= 2;
   curLvl += 1;   
}

How do i make the maxExp get multiplied by 2.5? it says i cant do that with a Int or something :i

Sorry if it is to much to ask for but, i have run into another problem, i wanna be able to hurt my enemy when i click on x
I got a code for this is playerlvl.cs (i got " public Enemyhp enemyhp;" variable at the top of the script :slight_smile: )

		if (Input.GetKey(KeyCode.X)){ 

        enemyhp.curHealth -= 10;   

        }

and this code in Enemyhp.cs(this should give me ecp when the hp reaches 0 ) (i got " public PlayerLvl playerlvl; " at the top)

		if(curHealth >= 0)
		playerlvl.curExp += 50;

It just says in the console that the object reference not set to an instance of an Object. :frowning:

Integers are whole numbers, so you either have to change maxExp to float, or cast the result as int, as so:

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

Input.GetKey gets called as long as the button is pressed down. You will want to replace that with something like Input.GetKeyDown() to only call once.

Then, you’re checking if (curHealth >= 0), which means if the health is bigger then 0. You want that to be (curHealth <= 0). You also want to destroy the gameObject after, if not, it will keep giving the player 50exp each frame.

Now, for the object reference not set to instance of object, that’s because you didn’t link your objects. You have two public variables, enemyhp and playerlvl. In the inspector, drag the player game object on top of playerlvl, and the enemy object on top of enemyhp.

It still says object reference not set to an instance of an Object. :confused:
And i have got the scripts in the inspector already :confused:

I’m not talking about draging and droping your scripts on top of game objects. I’m saying, now that you’ve done that, within the new scripts that you’ve placed you should link that as well.

If all else fails, when it gives you that error it will also provide a line nr. What is on that line nr?

My console says this
"NullReferenceException: Object reference not set to an instance of an object
PlayerStat.AddjustCurrentMagic (Int32 adj) (at Assets/Lol fun/PlayerStat.cs:145)
PlayerStat.Update () (at Assets/Lol fun/PlayerStat.cs:52)
"
but that has something to do with me working on stats since, i wouldent mind if you could help me fix that, as i believe its the same problem , the object reference thing :slight_smile:
These are my script :i

Playerlvl.cs

using UnityEngine;

using System.Collections;

 

public class PlayerLvl : MonoBehaviour {

    public int maxLvl = 75;

    public int curLvl = 01; 

    public float LevelLength;

    public int curExp = 0;

    public int maxExp = 150;
		
	public Enemyhp enemyhp;
 
	public PlayerStat playerstat;
	

    void Start () {

    LevelLength = Screen.width / 8;

    }

    

 

    void Update () {

    AddjustCurrentLvl(0);   

    AddjustCurrentExp(0);

        

    }

    

    

    void OnGUI(){

    GUI.Box(new Rect(10, 260, LevelLength, 20), curLvl + "/" + curExp); 

    }

    

    

   

    public void AddjustCurrentLvl(int adj) {

      curLvl += adj;    

        

        

    

        

        if(curLvl > maxLvl)

            curLvl = maxLvl;

        

        if(maxLvl < 1)

            maxLvl = 1;

    }

    

    

        public void AddjustCurrentExp(int adj) {

      curExp += adj;
        

        

        

        if(curExp >= maxExp)

        {

           curExp = curExp - maxExp;

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

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

        curExp += 10;   

        }
		
        if (Input.GetKeyDown(KeyCode.X)){ 

 

        enemyhp.curHealth -= 10;   

 

        }
        

    }   

}

Enemyhp.cs

using UnityEngine;
using System.Collections;

public class Enemyhp : MonoBehaviour {
	public int maxHealth = 50;
	public int curHealth = 50;
	
	public float healthBarLength;
	public PlayerLvl playerlvl;

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

	void Update () {
	AddjustCurrentHealth(0);
		
	}
	
	
	void OnGUI(){
	GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);	
	}
	
   
	public void AddjustCurrentHealth(int adj) {
	  curHealth += adj;	
		
		if(curHealth < 0)
			curHealth = 0;
		
		if(curHealth > maxHealth)
			curHealth = maxHealth;
		
		if(maxHealth < 1)
			maxHealth = 1;
		
		if(curHealth <= 0){
		playerlvl.curExp += 50;
		Destroy(gameObject);
	}
		healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
	}
}

Playerstat.cs

public class PlayerStat : MonoBehaviour {

    public int curStr = 0;
	
	public int maxStr = 500;

    public int curVit = 0; 
	
	public int maxVit = 500;

    public int curMagic = 0;
	
	public int maxMagic = 500;
		
	public Enemyhp enemyhp;
	
	public PlayerLvl playerlvl;
	
	public Playerhp playerhp;
	
	public Playermp playermp;
	
	public float StatLength;

    void Start () {
	StatLength = Screen.width / 8;

    }
	
		
		void OnGUI(){
    GUI.Box(new Rect(10, 240, StatLength, 20), curStr + "/" + curVit + "/" + curMagic); 
		
    }

    

 

    void Update () {

    AddjustCurrentStr(0);   

    AddjustCurrentVit(0);
		
	AddjustCurrentMagic(0);


	}

    

    

   

   // public void AddjustCurrentLvl(int adj) {

      //curLvl += adj;    
		
      //  if(curLvl > maxLvl)

    //        curLvl = maxLvl;

        

  //      if(maxLvl < 1)

//            maxLvl = 1;

  //  }

    

    

        public void AddjustCurrentStr(int adj) {
	
      curStr += adj;
		        if(curStr > maxStr)

            curStr = maxStr;

        

        if(maxStr < 1)

            maxStr = 1;

        
       //	if(playerlvl.curExp >= playerlvl.maxExp){
         //  	curStr += 15; 
	
   	   	//}
		

    }   
	        public void AddjustCurrentVit(int adj) {
		curVit += adj;
		
		
		if(curVit > maxVit)

            curVit = maxVit;

        

        if(maxVit < 1)

            maxVit = 1;
	

        
       	//if(playerlvl.curExp >= playerlvl.maxExp)

        //{
          // 	curVit += 15; 
	
        	//}
		

    }
	        public void AddjustCurrentMagic(int adj) {
		
		curMagic += adj;
		
		if(curMagic > maxMagic)

            curMagic = maxMagic;

        

        if(maxMagic < 1)

            maxMagic = 1;
      curMagic += adj;

        
       	if(playerlvl.curExp >= playerlvl.maxExp)

		{
           	curMagic += 15; 

        	}
	}

    

}

Right, so the problem wasn’t with the previous scripts.

When you copied the scripts here, the line numbers got messed up, so I don’t know exactly what that bug points to.

I’m guessing the problem might have something to do with if(playerlvl.curExp >= playerlvl.maxExp). You probably didn’t link the the player to the playerlvl variable in Playerstat.cs

do you mean “public Playerlvl playerlvl;” ?
Because i have done that :i

What is on lines 145 and 52 in Playerstat.cs exactly?

Are you trying to access a variable from another script?
If so, try reading the following for some ideas (the first ‘Gotcha’ specifically):
http://unitygems.com/mistakes1/

you should go finish watching the video first

AdrianC ; kinda fixed it, but it keeps saying it about this "
if (Input.GetKeyDown(KeyCode.X)){

GetComponent().curHealth = 10;
}

}

}"
with this console
"
NullReferenceException: Object reference not set to an instance of an object
PlayerLvl.AddjustCurrentExp (Int32 adj) (at Assets/Lol fun/PlayerLvl.cs:122)
PlayerLvl.Update () (at Assets/Lol fun/PlayerLvl.cs:38)
"
Slydog; Thanks man, it helped :stuck_out_tongue:
Nubz; what video?