I currently have a game where an enemy dies and he explodes into pieces of experience. The experience then moves towards the player and he absorbs them. I want to be able to set specific variables of the experience pieces when they are instantiated from the information the enemy held. The exact process that occurs is the enemy drops to 0 health, and then the experience bunch is instantiated, afterwards the enemy is destroyed. I want to be able to change the amount of experience is given when the enemy is a different level so for example at level one each xp piece is worth 5 points, and for a level 5 enemy each xp piece would be worth 15. How can i go about this is in a simple way? Ive had some experience in constructors when taking a programming class in high school but im not sure how i could implement that in this situation. Thanks!
While you instantiate your experience pieces, I guess you have a script attached to the prefab, grab that script and give it what you need:
public ExperienceScript:MonoBehaviour{
int _valueA;
int _valueB;
public int ValueA{get{return _valueA;}}
public int ValueB{get{return _valueB;}}
public void SetValues(int a, int b){
valueA = a;
valueB = b;
}
}
Now the script creating the pieces:
public GameObject expObject;
public void DestroyObjCreateExp(){
GameObject obj = (GameObject)Instantiate(expObject, position, rotation);
ExperienceScript sc = obj.GetComponent<ExperienceScript>();
if(sc != null) sc.SetValues(10,20);
}
So when you create the object you also assign a reference to it in a variable. From that variable you can now get the component and do whatever you need to do with it.
Note there are other ways to do that, you can make the variable public or make the properties get,set and so on.