Hello,
I’ll get straight to the point.
I have orbs across the map and when picked up, they should play a particle effect on the player for a few seconds (effect “traveling” with player). I have a few different effects and I’m not quite sure what’s the best location to put the particle effect prefab and what’s the easiest way to make it play from any script.
Any suggestions are welcome, because I have a feeling that my method down below is over complicating something really simple.
Problem:
So, I’ve made and dragged the particle prefabs onto my Player game object (it’s now a child of my Player) and disabled PlayOnAwake. Then I put a script on the particle prefab, so I can call the script anytime I want to play the particle effect. (I have 3 different particle effects that are realized just the same way, depending on the picked object)
This is the function that calls my script on the particle prefab (only last 2 lines of code matter):
```csharp
- void OnTriggerEnter(Collider orb)
{
if(orb.gameObject.CompareTag(“HealthOrb”))
{
orb.gameObject.SetActive(false);
currentHealth += 20;
if(currentHealth > startingHealth)
{
currentHealth = startingHealth;
}
healthSlider.value = currentHealth;
OrbPickUpPlay orbPickUp = new OrbPickUpPlay();
orbPickUp.Play(true); //is this even the right way to call the script?
}*
```
And this is the called script:
using UnityEngine;
using System.Collections;
public class OrbPickUpPlay : MonoBehaviour
{
ParticleSystem orbParticles;
AudioSource orbAudio;
void Start()
{
orbParticles = GetComponent<ParticleSystem>();
orbAudio = GetComponent<AudioSource>();
}
void Update()
{
}
public void Play(bool orbType)
{
//Start();
if(orbType == true)
{
orbParticles.startColor = new Color(100, 0, 0, .5f);
}
else if(orbType == false)
{
orbParticles.startColor = new Color(0, 0, 100, .5f);
}
orbParticles.Play();
orbAudio.Play();
}
}
I keep getting “Object reference not set to an instance of an object” or some “Null exception” no matter what I do. I’ve been programming for the last three weeks 10 hours a day, my brain isn’t working anymore.
Any help is much appreciated.