I have a problem with lighting my particle system with a 2D light. I found this script that adds 2D lights to the particles. It worked great: it created some lights and positioned them correctly, but they don’t light anything. A light that was created manually in the scene worked well, but a light with the same settings, instantiated via script, just isn’t working. Here is the script with a little debugging part:
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(ParticleSystem))]
public class AttachGameObjectsToParticles : MonoBehaviour
{
public GameObject m_Prefab;
ParticleSystem m_ParticleSystem;
private List<GameObject> m_Instances = new List<GameObject>();
private ParticleSystem.Particle[] m_Particles;
// Start is called before the first frame update
void Start()
{
m_ParticleSystem = GetComponent<ParticleSystem>();
m_Particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles];
}
// Update is called once per frame
void LateUpdate()
{
int count = m_ParticleSystem.GetParticles(m_Particles);
while (m_Instances.Count < count)
{
GameObject a = Instantiate(m_Prefab, m_ParticleSystem.transform);
if (a != null)
print("Instantiated sucssefuly");
else
print("Something wrong with instaniation");
m_Instances.Add(a);
}
bool worldSpace = (m_ParticleSystem.main.simulationSpace == ParticleSystemSimulationSpace.World);
for (int i = 0; i < m_Instances.Count; i++)
{
if (i < count)
{
if (worldSpace)
m_Instances[i].transform.position = m_Particles[i].position;
else
m_Instances[i].transform.localPosition = m_Particles[i].position;
m_Instances[i].SetActive(true);
}
else
{
m_Instances[i].SetActive(false);
}
}
}
}