Pass a variable into a class on creation
I've got a Mage class where they can shoot a fireball.
I've got it so that when the player decided to shoot a fireball a fireball will instantiate with a script on it that will deal with movement and damage of the fireball.
I don't want the player class to have to control or deal with the fireball in any way of than creating it.
Inside my player class is the variable _power. This variable is basically a modifier for the damage of the fireball (and later on all moves). There for my fireball class needs to know about this number to correctly apply the damage.
So, my question, is there any way to pass my _power variable into the fireball class when it is instantiated?
(Sorry for any bad formatting of my question, first time I've posted here)
class Mage::Player
{
public GameObject fireball;
private void useFireball() {
if (isFireBallOnCooldown){
return;
}
//instantiate fireball
setFireballCooldown();
}
}
Answer by Prosser95 · Jun 27, 2016 at 07:02 PM
Hi, I can't post a comment, I think you've misunderstood my problem but I've solver it now anyway thank you.
The answer to anyone else who may be needing this is below.
GameObject fb = Instantiate(fireball, transform.position, Quaternion.identity) as GameObject;
fb.GetComponent<Fireball>().setPower(_power);