Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Adding EXP from another source.

Discussion in 'Scripting' started by DeeJayVee, Feb 1, 2015.

  1. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Hey people;

    I've created an EXP script that seems to work pretty well, I'm satisfied with it, my problem is, I want to add exp from say a dead enemy, I'm having trouble;

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EXP : MonoBehaviour {
    5.     private int level = 1;
    6.     public int currentEXP = 20;
    7.     public int newEXP;
    8.     public int mobEXP;
    9.     public int reserveEXP;
    10.     private int maxEXP = 100;
    11.  
    12.  
    13.  
    14.  
    15.     void Start () {
    16.    
    17.     }
    18.    
    19.  
    20.  
    21.  
    22.     void Update () {
    23.    
    24.         AddEXP ();
    25.     }
    26.  
    27.  
    28.  
    29.  
    30.     void OnGUI()
    31.     {
    32.         GUI.Box (new Rect (10, 850,Screen.width - 30, 20), currentEXP + " / " + maxEXP);
    33.         GUI.Box (new Rect (10, 50, 50, 20), level.ToString());
    34.     }
    35.  
    36.  
    37.  
    38.     //Addsthe exp,  and creates a  reserve exp to add to the next level.
    39.  
    40.  
    41.     public void CalculateEXP(int mobEXP)
    42.     {
    43.         while ((mobEXP + currentEXP)  > maxEXP)
    44.         {
    45.             reserveEXP = (mobEXP + currentEXP) - maxEXP;
    46.             currentEXP += mobEXP;
    47.             CalculateLevel();
    48.             Debug.Log ("Working");
    49.             mobEXP = reserveEXP;
    50.             Debug.Log (reserveEXP);
    51.             reserveEXP = 0;
    52.  
    53.    
    54.         }
    55.  
    56.         currentEXP += mobEXP;
    57.         CalculateLevel ();
    58.     }
    59.  
    60.  
    61.  
    62.     //Makes sure  the level is correct, and allows for adding the reserve  exp.
    63.     void CalculateLevel()
    64.     {
    65.         if (currentEXP >= maxEXP) {
    66.             level += 1;
    67.             maxEXP *= 2;
    68.             currentEXP = 0;
    69.                 }
    70.     }
    71.  
    72.  
    73.  
    74.     //Debug purposes,adds EXP.
    75.  
    76.  
    77.     void AddEXP()
    78.     {
    79.         if(Input.GetKeyDown (KeyCode.F))
    80.         {
    81.             newEXP += 100;
    82.             CalculateEXP(newEXP);
    83.             newEXP = 0;
    84.         }
    85.  
    86.         if (Input.GetKeyDown (KeyCode.G)) {
    87.             newEXP+= 150;
    88.             CalculateEXP(newEXP);
    89.             newEXP = 0;
    90.                 }
    91.         if (Input.GetKeyDown (KeyCode.H)) {
    92.             newEXP+= 37;
    93.             CalculateEXP(newEXP);
    94.             newEXP = 0;
    95.                 }
    96.     }
    97. }
    98.  
    I've tried in another script to access it by using

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Addmobexp : MonoBehaviour {
    5.  
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         EXP giveEXP = new EXP();
    10.         giveEXP.CalculateEXP(400);
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.  
    16.     }
    17. }
    18.  
    But I get an error about not being able to use the keyword 'new' to add MonoBehaviors.


    Could you push me in the right direction?
     
  2. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    193
    in Addmobexp you'll need to reference the EXP class that already exists rather then create a new instance. You can either make this a public var and drag/drag the component in the inspector, or find it by code.

    Code (csharp):
    1. private EXP exp;
    2.  
    3. void Start()
    4. {
    5.      exp = gameObject.GetComponent<EXP>();
    6.      exp.CalculateEXP(400);
    7. }
     
  3. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Oh right, I see, I was able to make the public variable easy enough, I'm not sure how to find it by code yet, I think I've seen something like that, I will go look it up, thanks for your help, Crayz
     
  4. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    193
    My snippet above finds the component by code using gameObject.GetComponent<EXP>(). It'll work as long as EXP and Addmobexp components are both attached to the same object.

    If the components are attached to separate objects you'll need to reference the object with EXP attached. Something like this..

    Code (csharp):
    1. public GameObject someObject; // drag and drop the object with EXP attached to it in the inspector
    2. private EXP exp;
    3.  
    4. void Start()
    5. {
    6.      exp = someObject.GetComponent<EXP>();
    7.      exp.CalculateEXP(400);
    8. }
     
  5. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    You should use events. An event such as "EnemyDied" can be listened to by the EXP script.

    Ideally, you would provide extra data such as "EnemyDied(GameObject personWhoKilledEnemy)".

    Code (CSharp):
    1. public class Experience : Monobehaviour {
    2.  
    3.     public float currentExp;
    4.  
    5.     // Listen for event (when Died fires, OnEnemyDied will also fire because we add it)
    6.     void OnEnable
    7.     {
    8.         EnemyScript.Died += OnEnemyDied;
    9.     }
    10.     void OnDisable
    11.     {
    12.         EnemyScript.Died -= OnEnemyDied;
    13.     }
    14.  
    15.     void AddXP(int amount)
    16.     {
    17.         currentExp += amount;
    18.     }
    19.  
    20.     // This matches the EventHandler(int xp) delegate template
    21.     void OnEnemyDied(int xpToGive)
    22.     {
    23.         AddXP(xpToGive);
    24.     }
    25.  
    26. }
    Code (CSharp):
    1. public class EnemyScript : Monobehaviour {
    2.  
    3.     public delegate void EventHandler(int xp); // Events/functions have to match these parameters
    4.     public static event EventHandler Died;
    5.  
    6.     public int xpToGive = 10;
    7.  
    8.     void Die()
    9.     {
    10.         // Send/fire event
    11.         if (Died != null) Died(xpToGive);
    12.     }
    13.  
    14. }
     
  6. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Oh right, yes, they are on different objects, so I referenced the Player object, it worked like a charm :)

    This is a bit of information to soak in, I think I understand it, I will try to play around with it right now, thanks for your help