- Home /
Can't change change values of instantiated gameobject from another script
Hello. So here's the deal, i have a script that manages the spawn of about 5 different gameobjects, these gameobjects have a script attached called MobController, this script manages the hp, damage etc. Since i want to make different scenes, i would like to have an external script(MobOverride) that could alter these values on the prefabs as they are spawned.
so on the MobController script i have.
void Start () {
startingHealth = MobOverride.MO.spaceshipStartHp;
currentHealth = MobOverride.MO.spaceshipCurrentHp;
damage = MobOverride.MO.spaceshipDamage;
fireRate = MobOverride.MO.spaceshipFireRate;
xp = MobOverride.MO.spaceshipXp;
currentHealth = startingHealth;
}
which accesses the static MobOverride script, the script i want to use to change the values of the 5 different ships.
Then i have the MobSpawnManager that spawns gameobjects on a IEnumerator
Instantiate(enemy, spawnPosition, spawnRotation);
There's got to be a way to code this, otherwise i'm stuck with the option of copying 5x50 prefabs with different values which i really wouldn't want to.
Hope you guys can help, thanks.
Answer by Zodess · Mar 07, 2017 at 01:48 PM
void Start () {
gameControl = GameObject.FindGameObjectWithTag("GameController");
MO = gameControl.GetComponent<MobOveride>();
startingHealth = MO.spaceshipStartHp;
currentHealth = MO.spaceshipCurrentHp;
damage = MO.spaceshipDamage;
fireRate = MO.spaceshipFireRate;
xp = MO.spaceshipXp;
}
This seems to have done it, though it's not very elegant.