Changing Prefabs at Run time

Hi, im currently trying to work on changing a prefab on a gun at runtime. The prefab is the bullet that is instantiated when the user presses an input, however i am looking to change the bullet when getting a certain score in the game. e.g.

if (score <= 0){

(KEEP ORIGINAL PREFAB)

}

if (score <= 15){

(CHANGE TO NEW PREFAB - Enable/Disable statement?)

}

i apologise for the lack of coding i have shown for this, but i am just starting on the code and was wondering if changing Prefabs at run time is even possible, as i have seen mesh and material is changeable, i am going to look into enable/disable commands as it seems the best place to start!

cheers

This script is pretty basic, but it should get you started. I didn't include anything about how to increment your score, or how to send your bullet flying or anything like that. This is just a way to change your prefab like you asked about.

This assumes you’re using an object with a rigidbody for your projectiles.
Create an array that you can set the size of (however many different types of projectiles you want) and then drag/drop your projectiles into it from the editor->


public var projectile : Rigidbody[] ;

Then you’ll have your player score variable, however you contorl its value is some other function you’ll need to make->


@System.NonSerialized //<-this just keeps the playerScore out of the inspector panel
var playerScore : int = 0 ;

Now the Update function for when you call your firing function (Which in this case will be “Shoot()” ). You can change the button or key etc to whatever you want, but I’m just going with the default fire button which is the left control or left mouse. Input.GetButtonDown() only fires once each time the button is pressed.->

`
function Update(){
   if(Input.GetButtonDown("Fire1")) {
      Shoot() ;
   }
}
`

And then we go to the Shoot() function. This example assumes you’re using three different projectiles that you’ve drag/dropped into the array in the editor. You can easily modify it to different score values for the changes, or add/delete “else if” statements for more/less projectiles in your array. This could probably be done better with switch’s or for’s etc, but this was a quick script. …Anyway, here’s Shoot() →

`
function Shoot(){
   var clone : Rigidbody ;
      if (playerScore >= 50 ) { //if score is 50 or more
         clone = Instantiate(projectile[2], transform.position, transform.rotation) ;
      }
      else if(playerScore >= 15 && playerScore <= 49){ //if score is between 15 and 49
      clone = Instantiate(projectile[1], transform.position, transform.rotation) ;
      }
      else{ //if score is anything other than the above use default/beginner projectile
      clone = Instantiate(projectile[0], transform.position, transform.rotation) ;
      }
}
`

All of the above goes into one script, which would be attached to whatever object you’re instantiating at.
ie: an empty object placed in the front-center of your gun barrel.
This will instantiate your prefab(s) at that objects location, with said objects rotation. Hope this helps.