Search Unity

Best way to create a prefab with properties unique to that instance?

Discussion in 'Prefabs' started by matidfk, Jul 14, 2021.

  1. matidfk

    matidfk

    Joined:
    Jun 19, 2020
    Posts:
    50
    Hi, I've got a bullet prefab, that I'd like to be able to instantiate but with a different speed, direction etc every time. I've read up a bit on classes so I'm wondering if there's a way to do this like that. Also I'm not sure whether having a Game Manager empty with a bullet manager script, that i can then reference in the enemy object is a good way about going with all of this.
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    If you want the bullets to have random properties when they spawn, the easiest way is to have the bullet script set their starting properties to random values using something like Random.Range().

    That way, whenever a new bullet is spawned it will have different speed, direction, etc...

    If you want a different direction, but you'd like to control that direction, you could get a random integer between 0 and 3, and then create a switch statement with 4 different directions (up, down, left, right), and get a random direction that way. This could be applied to speed and loads of other different properties as well.

    These are just a few ways, hopefully they were of some help.

    Good luck!