Hey,
I have the problem that I am getting NullReferenceExceptions when I want to instantiate a sfx for the weapon.
For this I use a SFX-prefab that play only its parameter-given AudioClip:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof (AudioSource))]
public class SFX : MonoBehaviour {
private AudioSource aud;
void Start () {
aud = GetComponent<AudioSource>();
aud.spatialBlend = 1f;
}
public void selectSFX(AudioClip sound){
aud.clip = sound; // ERROR
aud.Play();
Destroy(gameObject, aud.clip.length);
}
}
Now I instantiate it in the Weapons-class:
GameObject sfxClone = Instantiate(Resources.Load("Prefabs/SFX"), bulletEmpty.position, transform.rotation) as GameObject;
sfxClone.GetComponent<SFX>().selectSFX(shootSound);
but this gives me the NullReferenceException in SFX, line with //ERROR
The shootSound-variable in the weaponScript is assigned.
Thanks in advance