Search Unity

how to spawn random object with different properties

Discussion in 'Scripting' started by alexchandriyaa, May 16, 2018.

  1. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    i need to spawn chests with random positions but each chest must have different fucntions like if i click first chest it will open in 1hr for the second chest it will open in 30mins like this for all chest the timer will vary and some functionalities too.. someone help me out
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    What is it you're asking about? Just different delays when clicked to open for each chest?
     
  3. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Spawn the item first, then set it's properties. Sounds like your opening is purely based on one floating point value (time until opening) that you can set in a factory.
     
  4. alexchandriyaa

    alexchandriyaa

    Joined:
    Jan 18, 2017
    Posts:
    140
    ya exactly.. spawning different objects with different delays and coins,gems inside the chest to collect
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    So you want a chest class and an item class.

    The chest class could have something like an initialisation method that will take the opening delay and a list of contents -
    void Init( float fOpenDelay, List< ChestItems > lItems )
    .

    Then just instantiate and initialise a load of those bad boys. :)

    Ok, so here you want to use polymorphism. So your chest class can have virtual methods in it, for example
    virtual public void DoMagicAction( )
    . Now you can create any number of specialist chests that implement their own
    DoMagicAction()
    .