Changing prefab script variable in runtime

Hey all!

I have a little bit of a problem here.

I’m making a game similar to asteroids, and ship will have several weapons.
When player holds “fire” button, it creates instance of the “Projectile_Laster_1” prefab for example.

Now, i used a plugin for a ingame console, and i’m trying to make it so that i can change how fast projectile goes via this console. Speed of the projectile is stored in variable in projectile.cs, which is attached to the prefab i mentioned.

Now to the actual problem. The problem is that i can’t figure out how to change that property of the script of the prefab.
In short, i’d like to basically say: “look, for all the projectiles from now on, for projectile speed use this value from the console”.

I hear that you can’t change prefab itself, but can you in some way say that from now on, if you make instance of this prefab, for the speed value of that script, use this new setting. Is that possible?

tnx,
Zocky

You can make a static variable called something like difficultyLevel. Then make your prefabs check the variable:

//Add this to some script. Call it 'ExternalScript'
static var difficultyLevel : int = 1;

//Add this to your asteroid prefab
function Update() {
    if (ExternalScript.difficultyLevel = 1) {
        mySpeed = 10;
    }
    else if (ExternalScript.difficultyLevel = 5) {
        mySpeed = 60;
    }
    //And so on...
}

Note you can name your script whatever you want or even declare this variable in any other script, but there must be ‘static’ before ‘var’!

Hope this helps :slight_smile: