playMaker

Author Topic: Change Material of pooled Prefab before spawn?  (Read 911 times)

curb47

  • Sr. Member
  • ****
  • Posts: 256
Change Material of pooled Prefab before spawn?
« on: January 05, 2021, 01:08:19 AM »
Hiya, hope you're all well.

I've got a brain stretcher...

My little spaceship fires lasers, which can be upgraded through 5 levels of power. Here it is at level 1, a single laser...


The laser object exists as a pooled (Pool Boss) prefab, and uses the Draw Line action...


With each level of upgrade I'd like to change the colour/material of the Prefab so the change in power level is clearly indicated in the gameplay.

I've got the power lever system sorted out, so as you upgrade, the damage done increases, the issue I'm having is I'd like the material to be updated, on all the Pooled objects before they are spawned, however, the objects are disabled (of course) and I can't seem to add them to an array or 'find' them. Know what I mean?


I thought I could just make 5 different Prefabs of the Laser (LASER1, LASER2, LASER3 etc) , each with a different colour/material and depending on the upgrade level, Pool the corresponding Prefab. I think this would work.

I guess I'd like to streamline the process, and if it's possible to just change the material, that would be better.

Thanks in advance.

J.

« Last Edit: January 06, 2021, 02:45:23 AM by curb47 »

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Change Material of pooled Prefab before spawn? [SOLVED]
« Reply #1 on: January 05, 2021, 05:35:36 AM »
I've surprised myself and managed to do it!

I set the Laser Prefab as an object variable, and the Material on the Draw Line as a variable, then simply set the Material variable according to the different Laser power.

Quite simple actually.

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Change Material of pooled Prefab before spawn?
« Reply #2 on: January 06, 2021, 02:48:02 AM »
Actually, scratch that last comment, it doesn't work properly. It's unreliable.

I think I need to add my Pooled LASER1 objects to an Array, then go through the array and set the Material/Color accordingly.

Or is there a way to set the Material/Color of the Prefab before it's pulled into the Pool?

Mmm. How do I do this? Thanks.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Change Material of pooled Prefab before spawn?
« Reply #3 on: January 06, 2021, 05:57:13 AM »
Hi.
On the laser you can have a fsm to handle the 'Level'

After you spawned a laser, do a 'Set Fsm Int' and set the level on the spawned prefab.

On the prefab fsm on Start you can do a Int Switch (every Frame) and check the 'Level'
You should use level 1 to x (not 0)

Then according to the level you can setup what you need for that level.

Before despawning use 'Set Int Value' and set the level value to 0
also disable objects that you might have enabled during the setup.

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Change Material of pooled Prefab before spawn?
« Reply #4 on: January 06, 2021, 09:39:18 AM »
Thanks djaydino,

I considered setting the the level/colour on the actual Prefab itself, but thought there might be a performance hit as the lasers fire very rapidly, and I wanted to minimise the amount of 'work' required.

I'll give it a shot and get back to you.

Thanks again.

J.