- Home /
How to create ParticleSystem shop?
So I`ve created shop for skins and now I would like to also add particle effect shop. My question is: how can I create it? I thought of adding all ParticleSystems(Which are SetActive(false) by default) to my Player gameObject and when player selects the needed Particle System effect simply set SetActive to true for that ParticleSystem. I've been told that adding multiple Particle Systems on one gameObject is a very heavy option and might seriously affect performance. Then I decided to create an array of ParticleSystems and then play them depending on which effects player chose, but the code doesn't work:
public ParticleSystem[] skins_ParticleEffects;
private void Start(){
skins[0].Play();
}
Would be just nice if you offered me how can I create this Particle System shop.
Answer by Harry_Drew · Jun 09, 2021 at 11:27 AM
Hi, there is many ways to approach this. I would create prefabs of each of the particle systems and instantiate the relevant one at runtime. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ParticleSystemController : MonoBehaviour
{
public GameObject[] ParticleSystemPrefabs;
public Vector3[] LocationsForParticleSystem;
void Start()
{
int index = PlayerPrefs.GetInt("ParticleSelectionIndex", 0);
for (int i = 0; i < LocationsForParticleSystem.Length; i++)
{
Instantiate(ParticleSystemPrefabs[index], LocationsForParticleSystem[i], Quaternion.identity);
}
}
//CALL THIS TO SET THE INDEX OF THE PARTICLE SYSTEM YOU WANT TO USE
public void StoreParticleSystemIndex(int index)
{
if (index < ParticleSystemPrefabs.Length && index >= 0)
{
PlayerPrefs.SetInt("ParticleSelectionIndex", index);
}
else
{
Debug.LogError("Cannot assign particle index because it's out of range");
}
}
}
This is just one way of achieving this sort of system :)
Thank you for your response. Just a small thing: the particle effects must follow my player. How can I make the ParticleSystem prefab which is instantiated by Instantiate() to follow the player?
UPD: I found how can I make the prefab follow the player. I just attached a script to all prefabs, where in Start method I simply set parent of the prefab to my player, which automatically makes it follow the player:
private void Start(){
transform.parent = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>().transform;
}
Edited to let you do that:
public class ParticleSystemController : MonoBehaviour { public GameObject[] ParticleSystemPrefabs; public Vector3[] LocationsForParticleSystem; public Transform Player;
void Start()
{
int index = PlayerPrefs.GetInt("ParticleSelectionIndex", 0);
for (int i = 0; i < LocationsForParticleSystem.Length; i++)
{
GameObject ob = Instantiate(ParticleSystemPrefabs[index], LocationsForParticleSystem[i], Quaternion.identity) as GameObject;
ob.SetParent(Player);
}
}
//CALL THIS TO SET THE INDEX OF THE PARTICLE SYSTEM YOU WANT TO USE
public void StoreParticleSystemIndex(int index)
{
if (index < ParticleSystemPrefabs.Length && index >= 0)
{
PlayerPrefs.SetInt("ParticleSelectionIndex", index);
}
else
{
Debug.LogError("Cannot assign particle index because it's out of range");
}
}
}